在视图之间共享变量时出现问题 - 缺少某些内容?

发布于 2024-08-29 11:00:05 字数 1689 浏览 12 评论 0原文

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

首先..我有两个 .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];
}

并在top

#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)

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

干杯

萨姆

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韶华倾负 2024-09-05 11:00:05

您在 -viewDidLoad 中初始化了一个新的 MemoryViewController 实例,因此它的所有实例变量当然都是 0nil >。如果您已经有一个需要从中获取属性的 MemoryViewController,则需要引用该实例而不是创建一个新实例。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文