加载视图时 NSInteger 值发生变化

发布于 2024-12-25 10:41:32 字数 379 浏览 4 评论 0原文

我的应用程序委托中有一个名为 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 技术交流群。

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

发布评论

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

评论(1

半葬歌 2025-01-01 10:41:32

我同意这个评论。确保您的 appDelegate 属性不为空。如果是,您可以使用

id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate 

它再次获取它。

[编辑]
您必须将 appDelegate 转换为您的类类型才能从委托获取“healthInt”。

MyAppDelegate *myApp = (MyAppDelegate *)[UIApplication sharedApplication].delegate

[第二次编辑]
呃!我没有看到你的示例代码在你的 viewDidLoad 中。热舔是对的。您需要在视图控制器中设置委托,然后才能调用它。

试试这个:

-(void)viewDidLoad{

appDelegate = [[UIApplication sharedApplication] delegate];
     if(appDelegate.healthInt <= 0){
     healthLabel.text = @"Dead";
    }
    }

希望这有帮助。

I agree with the comment. Make sure that you appDelegate property isn't null. If it is you can use

id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate 

to get it again.

[Edit]
You will have to cast the appDelegate to your class type to get "healthInt" from the delegate.

MyAppDelegate *myApp = (MyAppDelegate *)[UIApplication sharedApplication].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:

-(void)viewDidLoad{

appDelegate = [[UIApplication sharedApplication] delegate];
     if(appDelegate.healthInt <= 0){
     healthLabel.text = @"Dead";
    }
    }

Hope this helps.

Rob

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