加载视图时 NSInteger 值发生变化
我的应用程序委托中有一个名为 healthInt 的 NSInteger。在应用程序委托中,我将 healthInt 设置为 100。当应用程序加载第一个视图时,healthInt 仍然等于 100。但是当它加载下一个视图时,healthInt 设置为 0。即使该视图控制器中的标签(标题为healthLabel)显示 100。这是一个问题的原因是因为我想做的是
-(void)viewDidLoad{
if(appDelegate.healthInt <= 0){
healthLabel.text = @"Dead";
}
}
但这似乎不起作用。我知道这是一个糟糕的架构,但对我来说重做整个项目有点太晚了。如果您需要查看其他代码,我很乐意向您展示:)
I have an NSInteger in my app delegate called healthInt. In the app delegate i have healthInt set to 100. And when the application loads the first view healthInt is still equal to 100. But when it loads the next view, healthInt is set to 0. Even though a label in that view controller (titled healthLabel) displays 100. The reason this is a problem is because what i wanna do is
-(void)viewDidLoad{
if(appDelegate.healthInt <= 0){
healthLabel.text = @"Dead";
}
}
But this doesn't seem to work. And i know this is bad architecture but it's a bit too late for me to redo the entire project. If you need to see other code i'll be happy to show it to you :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我同意这个评论。确保您的 appDelegate 属性不为空。如果是,您可以使用
它再次获取它。
[编辑]
您必须将 appDelegate 转换为您的类类型才能从委托获取“healthInt”。
[第二次编辑]
呃!我没有看到你的示例代码在你的 viewDidLoad 中。热舔是对的。您需要在视图控制器中设置委托,然后才能调用它。
试试这个:
希望这有帮助。
抢
I agree with the comment. Make sure that you appDelegate property isn't null. If it is you can use
to get it again.
[Edit]
You will have to cast the appDelegate to your class type to get "healthInt" from the delegate.
[2nd Edit]
DUH! I didn't see your example code was in your viewDidLoad. Hot Licks is right. You need to set the delegate in the view controller before you can call it.
Try this:
Hope this helps.
Rob