Greg’s Online CS 193P Resources

May 13, 2009

Passing Data Between View Classes Tutorial – Presence #2

Filed under: cs 193p — gregorykaiser @ 5:06 am
Tags:

I got some e-mail again about passing data so I made this tutorial. There are really very few changes needed but they are spread across 6 files and if you miss some of them it can be hard to track down. As you follow these steps make sure and learn the pattern of #imports, ivar declaration and @property statements in the class .h file and the corresponding @synthesize statement needed in the class .m file.

Goal: Start with the PushAndPop.zip Demo code from CS193P and add passing of a Model’s Data (ModelData) object back and forth between the FirstViewController and the SecondViewController. When the “Push button” is pressed the new data will be passed to the second view screen, modified and returned to the FirstViewController.

To keep the demo changes simple I will not use IB to modify the screens so NSLog output to the Console will be used to verify the results.

1. Unzip and Build&Go the Demo to verify it works.

2. Create a new NSObject subclass called “ModelData” (you need both .h and .m files) which is the data we will pass. This object will contain two text fields.

//
//  ModelData.h
//  PushAndPop - add data passing

#import <Foundation/Foundation.h>


@interface ModelData : NSObject {
	NSString *twitterId;
	NSString *displayName;
}
@property (nonatomic, retain) NSString *twitterId;
@property (nonatomic, retain) NSString *displayName;

- (id)initWithName:(NSString *)aTwitterId
	   displayName:(NSString *)aDisplayName;

@end

=======================
//
//  ModelData.m - add data passing
//  PushAndPop
//

#import "ModelData.h"

@implementation ModelData
//added stuff
@synthesize twitterId;
@synthesize displayName;

- (id)initWithName:(NSString *)aTwitterId displayName:(NSString *)aDisplayName; {
	self.twitterId = aTwitterId;
	self.displayName = aDisplayName;
	return self; //?
}

@end

3. Change FirstViewController to “see” this new data object and have a place to store it. We will change the -viewDidLoad method to create a ModelData object (“data”) and the -push method to send it to the SecondView Controller.

3.1 Modify FirstViewController.h to import DataModel and create a new ivar called “data” to hold an instance of it. Add the lines with the “// gk” comment.

//
//  FirstViewController.h
//  PushAndPop
//

#import <UIKit/UIKit.h>
#import "ModelData.h" //gk


@interface FirstViewController : UIViewController {
	ModelData *data;  //gk
}
@property (nonatomic, retain) ModelData *data;  //gk

- (IBAction)push:(id)sender;

@end

3.2 Now in FirstViewController.m make these changes (look for // gk). They create “data” to hold the ModelData object and change the SecondViewController initialization call to pass the object to the new view.

@implementation FirstViewController  
@synthesize data;  //gk - add here after @implementation statement
=======================
- (IBAction)push:(id)sender  //gk - modify this method
{
    SecondViewController *secondViewController = [[SecondViewController alloc] initWithText:@"Something" passedData:data];  //gk
	data.displayName = @"BeforePushing"; //gk
	NSLog(@"In FirstViewController -push (Before Nav Push):PassedData=%@, %@",data.twitterId, data.displayName );  //gk
    [self.navigationController pushViewController:secondViewController animated:YES];
	NSLog(@"In FirstViewController -push (after Nav Push):PassedData=%@, %@",data.twitterId, data.displayName );  //gk
    [secondViewController release];
}
=======================
// add at top of -viewDidLoad method after   the   [super viewDidLoad] statement
	data =[[ModelData alloc] initWithName:@"Greg" displayName:@"NODisplayName"]; // gk
// add at the bottom of -viewDidLoad   
    NSLog(@"Leaving FirstViewController -viewDidLoad:PassedData=%@, %@",data.twitterId, data.displayName );  //gk
    
 

4. Change SecondViewController to “see” this new data object. We will also need to add the object to the -initWithText method and change -viewDidLoad to modify the data object.

4.1 Modify SecondViewController.h to import DataModel and create a new ivar called “somePassedData” to hold an instance of it. (Add the lines with the “// gk” comment).

//
//  SecondViewController.h
//  PushAndPop
//

#import <UIKit/UIKit.h>
#import "ModelData.h"  //gk


@interface SecondViewController : UIViewController {
    UILabel *label;
    NSString *text;
	ModelData *somePassedData;  //gk
	
}

- (id)initWithText:(NSString *)text passedData:(ModelData *)somePassedData;  //gk

@property (retain) IBOutlet UILabel *label;
@property (retain) NSString *text;
@property (retain) ModelData *somePassedData;  //gk
@end

4.2 Modify SecondViewController.m to add the object to the class interface and set it. (Add the lines with the “// gk” comment).

//
//  SecondViewController.m
//  PushAndPop
//

#import "SecondViewController.h"

@implementation SecondViewController

@synthesize label;
@synthesize text;
@synthesize somePassedData;  //gk

- (id)initWithText:(NSString *)someText passedData:(ModelData *)dataParameter  //gk
{
    if (self = [self initWithNibName:@"SecondView" bundle:nil]) {
        // Custom initialization
        self.title = @"Second";
        self.text = someText;
		self.somePassedData = dataParameter;  //gk
		NSLog(@"In SecondViewController -initWithText:Text=%@ PassedData=%@, %@", self.text, self.somePassedData.twitterId, self.somePassedData.displayName );  //gk
		self.somePassedData.displayName = @"SET in SecondViewController"; //gk  
    }
    return self;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	NSLog(@"Entry to-> SecondViewController -viewDidLoad=%@ PassedData=%@, %@", self.text, self.somePassedData.twitterId, self.somePassedData.displayName );  //gk
	self.somePassedData.displayName = @"SET in SecondViewController-ViewDidLoad"; //gk  //maynotwor

    self.label.text = self.text;
}

- (void)dealloc {
    [label release];
    [text release];
    
    [super dealloc];
}


@end

5.0 Once you have made these changes and start the application your Console will show

2009-05-12 18:38:33.352 PushAndPop[11471:20b] Leaving FirstViewController -viewDidLoad:PassedData=Greg, NODisplayName

When you click the “Push button” the Console will show

2009-05-12 18:39:08.562 PushAndPop[11471:20b] In SecondViewController -initWithText:Text=Something PassedData=Greg, NODisplayName
2009-05-12 18:39:08.563 PushAndPop[11471:20b] In FirstViewController -push (Before Nav Push):PassedData=Greg, BeforePushing
2009-05-12 18:39:08.565 PushAndPop[11471:20b] Entry to-> SecondViewController -viewDidLoad=Something PassedData=Greg, BeforePushing
2009-05-12 18:39:08.567 PushAndPop[11471:20b] In FirstViewController -push (after Nav Push):PassedData=Greg, SET in SecondViewController-ViewDidLoad

You can bounce between the screens to see the SecondView created and initialized as you like.

About these ads

3 Comments »

  1. Where is the zip file?

    Comment by John — May 14, 2009 @ 11:04 am | Reply

  2. Hi John-

    Look for 07-PushAndPop.zip under “Example Projects” at

    http://www.stanford.edu/class/cs193p/cgi-bin/downloads.php

    Greg

    Comment by gregorykaiser — May 14, 2009 @ 12:51 pm | Reply

  3. Gregory, Just wanted to say Thanks for a great blog of yours. I am a member of Auditors group, but still find a lot of useful materials in your blog. I am a little behind with the class – currently finishing Presence 1. Angela.

    Comment by Angela — June 24, 2009 @ 2:24 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Rubric Theme Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: