Will Youmans
Will Youmans

Reputation: 27

Update UILabel with Global NSString

In my delegate i have an NSInteger healthInt; and NSMutableString * healthString. In appDelegate.m i have set healthString to hold the changing value of healthInt. Then in a different view i have UILabel * healthLabel. And I have set healthLabel to display healthString with the following code.

    healthLabel.text = [NSString stringWithFormat:@"%@", appDelegate.healthString];

This works and displays the number 100, which is what i have set healthInt to in the appDelegate. But when a UIImageView mainSprite collides with a different ImageView healthInt should decrease by two. Which it does, i can tell because i can see it happen in the log. The log may change and display the values of the slowly decreasing healthInt, but the healthLabel doesn't update as healthInt decreases. My question is how can i get this healthLabel to update as healthInt decreases? I have tried just putting in that code for the collision detection between mainSprite and the other ImageView that causes damage but that doesn't seem to work. Thanks!

Upvotes: 0

Views: 244

Answers (1)

Michael Dautermann
Michael Dautermann

Reputation: 89509

If you want to get your UILabel to update every time healthInt or healthString changes, there's a couple ways to do this.

One way is to go into the setter method for healthInt (or create one instead of using @synthesize) and broadcast a NSNotification for each time your healthInt number changes. And then when the view with that label is visible, have an observer registered pick up on that notification and make a change to your label.

Another way is to use Key Value Coding & bind the UILabel's text field to healthString. Here is a related question that may help you use this possible solution.

Upvotes: 2

Related Questions