0

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

Stewbob
  • 16,759
  • 9
  • 63
  • 107
  • 1
    I'll bet you that `appDelegate` is nil. Probably because you're in a different instance of your view controller from the one where you initially set `appDelegate`. – Hot Licks Jan 06 '12 at 00:32
  • But healthInt is 100 in every other view controller besides this specific one. – Will Youmans Jan 06 '12 at 00:36
  • Read what I said again. You never even access `healthInt` because `appDelegate` is nil. It doesn't set itself -- you set it somewhere, and you didn't set it in that view controller. – Hot Licks Jan 06 '12 at 01:09

1 Answers1

1

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

Rob
  • 4,149
  • 5
  • 34
  • 48
  • I have this appDelegate = [[UIApplication sharedApplication]delegate]; In my viewDidLoad method, will that do the trick? And that id bit of code, where should that be? – Will Youmans Jan 06 '12 at 00:48
  • Yes that should work fine. I would then place a breakpoint on your code above and check that appDelegate is not nil. If it is not, I would look for code that sets healthInt value to some number. It is possible you have some code that is accidentally setting it to zero. – Rob Jan 06 '12 at 00:58
  • But where should i put that id appDelegate = [UIApplication sharedApplication].delegate should i put it in the appDelegate somewhere? – Will Youmans Jan 06 '12 at 01:07
  • @WillYoumans -- It's not going to do much good to put the code to extract the app delegate address into the app delegate. – Hot Licks Jan 06 '12 at 01:13
  • Sorry, you don't need it. I wrote the call in a generic way and it really wasn't very helpful. I realized after I posted it that using the type "id" would not allow you to see you healthInt property. So I added the second bit of code that was more realistic for what you would need. However, you said that you have the call in your viewDidLoad method and that should be all you need. Now I would look to see if something is setting your value that you didn't intend. – Rob Jan 06 '12 at 01:15
  • Yeah i've been looking all over. Maybe it's a bug with Xcode possibly. – Will Youmans Jan 06 '12 at 01:19
  • NSLog is your friend. Before you access `appDelegate.healthInt`, do `NSLog(@"appDelegate = %@", appDelegate);`. I'm guessing it will print `appDelegate = (nil)`. – Hot Licks Jan 06 '12 at 01:53