views:

63

answers:

1

Hi there

I know im missing something but my friend and I can figure out what.

Firstly.. I have two .hs and .ms that I'd like to share data between - two view controllers In the first .h i have this - that makes the variables and properties them

 //top half of .h


//Passing to Submit Page
NSMutableString *messageString; 
NSInteger theirTime;


}
@property (nonatomic, readwrite) NSInteger theirTime;
@property (nonatomic, retain, readwrite) NSMutableString *messageString;
/actions
@end

Then in the respective .m - sythesize them

@synthesize messageString, theirTime;

then from the new .h and .h i need to acces them.. so In view did load i do this

- (void)viewDidLoad {

messageString = [[NSMutableString alloc] init];

MemoryViewController *controller = [[MemoryViewController alloc] init];

timeInSeconds = controller.theirTime;

NSLog(@"Time = %d", timeInSeconds);
messageString = controller.messageString;
NSLog(@"Message - %@", messageString);
[controller release];

NSUserDefaults *HighScore = [NSUserDefaults standardUserDefaults];

bestTime.text= [NSString stringWithFormat:@"Best Time:%d", [HighScore integerForKey:@"integerKey"]];

currentTime.text = [NSString stringWithFormat:@"Current Time:%d", timeInSeconds];

[super viewDidLoad];
}

and at the top

#import "MemoryViewController.h"

and now the .h to show you all what the variables are

IBOutlet UILabel *bestTime;
IBOutlet UILabel *currentTime;
int timeInSeconds;
NSMutableString *messageString; 

So. In short - I made variables made properties, and synthesized them, then in the view i make an instance of the other VC, then try use them to do things

Log out put

2010-04-15 20:53:09.105 Memory[3538:207] Time = 0
2010-04-15 20:53:09.107 Memory[3538:207] Message - (null)

Any ideas guys would be great... if you need more code/ less code just say.. ive tried other blogs but they all do it with app delegates.. and i dont like global variables.

Cheers

Sam

+1  A: 

You initialised a new MemoryViewController instance in your -viewDidLoad, so of course all of its instance variables are 0 or nil. If you already have a MemoryViewController that you need to get the properties from, you need to reference that instance instead of creating a new one.

Graham Lee
can you please elaborate on that? Im not really understanding it. I have a MemoryViewController.m and .h and i import the .h into the new viewcontroller, SubmitPage.m..how do i reference it?
Sam Jarman
@Sam: pass it in when you initialise your other object, set up an IBOutlet, retrieve it from a parent view controller, the choices are boundless :-)
Graham Lee
i ended up passing them a different way.. using NSUserDefaults. But thanks!
Sam Jarman