0

我的应用程序委托中有一个名为 healthInt 的 NSInteger。在应用程序委托中,我将 healthInt 设置为 100。当应用程序加载第一个视图时,healthInt 仍然等于 100。但是当它加载下一个视图时,healthInt 设置为 0。即使该视图控制器中的标签(标题为healthLabel) 显示 100。这是一个问题的原因是因为我想做的是

-(void)viewDidLoad{
   if(appDelegate.healthInt <= 0){
   healthLabel.text = @"Dead";
  }
}

但这似乎不起作用。而且我知道这是一个糟糕的架构,但对我来说重做整个项目有点太晚了。如果您需要查看其他代码,我很乐意向您展示:)

4

1 回答 1

1

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

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

再次得到它。

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

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

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

尝试这个:

-(void)viewDidLoad{

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

希望这可以帮助。

于 2012-01-06T00:42:39.763 回答