0

我有兩個筆尖文件,並從第一個筆尖文件,同時攻絲Add按鈕導航欄上我搬到另一個和第二個視圖我再次有導航欄Save按鈕,我在viewDidLoad事件中創建如下:UINavigationItem - 無法識別選擇器

btnAdd = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self.navigationItem action:@selector(saveRecord:)]; 
self.navigationItem.rightBarButtonItem = btnAdd; 
self.navigationItem.title = @"Add Information"; 
[btnAdd release]; 

而且我已經創建的方法saveRecord如下

-(IBAction) saveRecord: (id)sender 
{ 
    NSLog(@"Save Button Tapped"); 
} 

但是運行程序下面的錯誤自帶說,它無法找到saveRecord方法之後。即使這個米ethod也沒有用的斷點稱爲..

錯誤

2011-08-27 11:48:25.813 BarcodeDemo[1903:207] -[UINavigationItem saveRecord:]: unrecognized selector sent to instance 0x5558660 
2011-08-27 11:48:25.817 BarcodeDemo[1903:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationItem saveRecord:]: unrecognized selector sent to instance 0x5558660' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x012f95a9 __exceptionPreprocess + 185 
    1 libobjc.A.dylib      0x0144d313 objc_exception_throw + 44 
    2 CoreFoundation      0x012fb0bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 
    3 CoreFoundation      0x0126a966 ___forwarding___ + 966 
    4 CoreFoundation      0x0126a522 _CF_forwarding_prep_0 + 50 
    5 UIKit        0x003744fd -[UIApplication sendAction:to:from:forEvent:] + 119 
    6 UIKit        0x00586cc3 -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 156 
    7 UIKit        0x003744fd -[UIApplication sendAction:to:from:forEvent:] + 119 
    8 UIKit        0x00404799 -[UIControl sendAction:to:forEvent:] + 67 
    9 UIKit        0x00406c2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527 

和一些堆棧的推移..........

任何一個可以幫助我.. 。我忘了什麼????????

回答

1

target對象其中動作定義。這裏的目標應該是self,而不是self.navigationItem

target:self 
2

不要使用target:self.navigationItem,或saveRecord:方法將被髮送到UINavigationItem(這是你得到的錯誤)。你可能只想要target:self

0

試試這個

UIBarButtonItem * btnAdd = [[UIBarButtonItem alloc] 
            initWithTitle:@"Add Information" 
            style:UIBarButtonItemStyleBordered 
            target:self 
            action:@selector(saveRecord:)]; 
self.navigationItem.rightBarButtonItem = btnAdd; 
[btnAdd release];