2011-12-27 70 views
0

在我的代表我有一個NSInteger healthInt;和NSMutableString * healthString。在appDelegate.m中,我設置了healthString來保存healthInt的改變值。然後在另一個視圖中我有UILabel * healthLabel。我已經設置了healthLabel以下面的代碼顯示healthString。更新UILabel與全球NSString

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

這個工程並顯示數字100,這是我已經設置healthInt在appDelegate。但是,當一個UIImageView mainSprite與另一個ImageView healthInt碰撞時,應該減少兩個。它能做什麼,我可以說,因爲我可以看到它發生在日誌中。日誌可能會更改並顯示慢速減少的healthInt的值,但healthLabel不會隨着healthInt的降低而更新。我的問題是我如何得到這個healthLabel更新爲healthInt減少?我試圖在mainSprite和其他ImageView之間的碰撞檢測代碼中引起損壞,但似乎不起作用。謝謝!

回答

2

如果你想讓你的UILabel每次更新healthInthealthString更改,有幾種方法可以做到這一點。

一種方法是進入設置方法healthInt(或創建一個而不是使用@synthesize),並且每次您的healthInt編號更改時廣播NSNotification。然後,當帶有該標籤的視圖可見時,請註冊的觀察員拿起該通知並對標籤進行更改。

另一種方法是使用鍵值編碼&將UILabel的文本字段綁定到healthStringHere is a related question that may help you use this possible solution

+0

你能推薦一個教程來演示你建議的第一種方法嗎? – 2011-12-27 01:08:02

+0

查看[關於通過NSNotificationCenter發送和接收消息的問題](http://stackoverflow.com/questions/2191594/how-to-send-and-receive-message-through-nsnotificationcenter-in-objective-c )... – 2011-12-27 01:12:29

+0

是的,我看着它,我真的不明白。我如何使用這種方法來更新UILabel。我應該把它放在appDelegate中,還是應該在其他類中做? – 2011-12-27 01:27:59