UINavigationItem - 无法识别的选择器

时间:2011-08-27 06:26:00

标签: iphone objective-c cocoa-touch uinavigationcontroller uinavigationbar

我有两个nib文件和第一个nib文件,同时点击导航栏上的Add按钮我移动到另一个和第二个视图我再次有导航栏Save按钮我在viewDidLoad中创建事件如下:

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方法..即使这个方法也没有用Breakpoints调用..

错误

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

还有一些堆栈继续..........

任何人都可以帮助我...我忘了什么????????????

3 个答案:

答案 0 :(得分:2)

不要使用target:self.navigationItem,否则saveRecord:方法将被发送到UINavigationItem(这是你得到的错误)。您可能只想要target:self

答案 1 :(得分:1)

target对象,其中定义了操作。目标应该是self,而不是self.navigationItem

target:self

答案 2 :(得分:0)

试试这个

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