1

我知道我错过了一些东西,但我的朋友和我可以弄清楚是什么。

首先..我有两个 .hs 和 .ms 我想在它们之间共享数据 - 两个视图控制器在第一个 .hi 有这个 - 这使得它们的变量和属性

 //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

然后在各自的 .m 中 - 合成它们

@synthesize messageString, theirTime;

然后从新的 .h 和 .hi 需要访问它们..所以考虑到加载我这样做

- (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];
}

并在顶部

#import "MemoryViewController.h"

现在 .h 向您展示所有变量是什么

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

所以。简而言之 - 我将变量设为属性,并合成它们,然后在视图中创建另一个 VC 的实例,然后尝试使用它们来做事

登出放

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

任何想法都会很棒...如果您需要更多代码/更少代码,请说..我尝试过其他博客,但他们都使用应用程序代表来做......而且我不喜欢全局变量。

干杯

山姆

4

1 回答 1

1

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.

于 2010-04-15T09:31:22.377 回答