According to Apple's docs, the value for currentTitle
may be nil.
它可能只是沒有設置。
你總是可以做if (myObject == nil)
檢查,或者在這種情況下:
-(IBAction) startMotion: (id)sender {
NSLog(@"Forward or back button is being pressed.");
UIButton * buttonName = (UIButton *) sender;
if (buttonName != nil) {
NSString *title = buttonName.currentTitle;
NSLog(@"Button Name: %@", title);
}
}
另一種方法來檢查,如果按下後退或前進按鈕,就是檢查id
本身。
//in the interface, and connect it up in IB
//IBOutlet UIButton *fwdButton;
//IBOutlet UIButton *bckButton;
-(IBAction) startMotion: (id)sender {
NSLog(@"Forward or back button is being pressed.");
UIButton * buttonName = (UIButton *) sender;
if (buttonName == fwdButton) {
NSLog(@"FWD Button");
}
if (buttonName == bckButton) {
NSLog(@"BCK Button");
}
}
此外,請確保您的網點和操作都在IB中連接,並保存並重新構建項目。我已經去了我在IB改變過的地方,保存了.m
文件(不是筆尖),並且像「爲什麼這不起作用?」
這是......客觀C?您應該將目標語言添加到標籤中,以便最好地瞭解該語言的人可以爲您提供幫助。 – 2010-11-12 19:23:57