开发者

Problem with sharing variables between views - missing something?

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 (nonat开发者_如何学运维omic, 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜