【问题标题】:UINavigationItem - unrecognized SelectorUINavigationItem - 无法识别的选择器
【发布时间】:2011-08-27 06:25:59
【问题描述】:

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

错误

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

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

谁能帮帮我...我忘记了什么????????

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      targetobject,其中定义了 action。这里的目标应该是self,而不是self.navigationItem

      target:self
      

      【讨论】:

        【解决方案3】:

        试试这个

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

        【讨论】:

          猜你喜欢
          • 2018-01-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多