在Objective-C中,我有一個簡單的代碼塊,每次按下按鈕時都會遞增計數器。我的日誌,甚至更新了界面,顯示增量爲4而不是1。這只是我的格式(我正在使用%d)或其他我缺少的東西顯示問題?我的猜想在於「%d」,但我是Objective-C的新手,但並不確定。 (注意,我也嘗試過「反+ = 1;」具有相同的結果在Objective-C中,爲什麼這會增加4而不是1?
int counterValue = 0;
NSLog(@"Count at init: %d",counterValue);
...
-(IBAction)pushButton {
NSLog(@"Count (Pre-Push) = %d",counterValue);
counterValue++;
NSLog(@"Count (Post-Push) = %d",counterValue);
}
輸出如下:
2010-02-20 18:39:39.125 My App[37536:207] Count at init: 0
2010-02-20 18:39:39.845 My App[37536:207] Count (Pre-Push) = 0
2010-02-20 18:39:39.846 My App[37536:207] Count (Post-Push) = 4
2010-02-20 18:39:40.165 My App[37536:207] Count (Pre-Push) = 4
2010-02-20 18:39:40.166 My App[37536:207] Count (Post-Push) = 8
2010-02-20 18:39:40.727 My App[37536:207] Count (Pre-Push) = 8
2010-02-20 18:39:40.728 My App[37536:207] Count (Post-Push) = 12
一個nit:Action方法應該是' - (IBAction)doWhatever:(id)sender',即使你不使用sender參數。 – Wevah
@ Wevah:不是。儘管單參數形式是最流行的,但所有這些方法簽名對於動作方法同樣有效:(1)' - (void)action',(2)' - (void)action:(id)sender',( 3)' - (void)動作:(id)sender forEvent:(UIEvent *)event'。資料來源:http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIControl_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006779-RH2-SW28 –
啊,你是對的。另一個UIKit/AppKit的區別很明顯。 (改變你的正確性;)) – Wevah