【问题标题】:Unrecognized selector sent to instance (with UIBarButtonItem)无法识别的选择器发送到实例(使用 UIBarButtonItem)
【发布时间】:2013-05-23 09:16:33
【问题描述】:

在我的 iPhone 应用程序中,我有这样的代码:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Instruction Controller";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(menuButtonClicked)];   
}   


#pragma mark private methodes

-(void) menuButtonClicked{
    NSLog(@"menuButtonClicked");
}

但是当我点击这个按钮时,它会引发一个异常:“无法识别的选择器发送到实例”。我该如何解决这个问题?

更新

2013-05-23 12:21:33.182 CICDP[3091:11603] -[__NSCFString menuButtonClicked]:无法识别的选择器发送到实例 0x8128eb0 2013-05-23 12:21:33.188 CICDP[3091:11603] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFString menuButtonClicked]:无法识别的选择器发送到实例 0x8128eb0”

【问题讨论】:

  • 选择器是什么?
  • @selector(menuButtonClicked) - 你可以在我的帖子中看到它
  • 但是控制台中的错误说明了什么?
  • 你能发布完整的堆栈跟踪吗?检测到什么实例,什么选择器
  • 我已经提供给你了

标签: iphone ios


【解决方案1】:

看起来您的控制器类(按钮操作的目标)在按钮触发之前已被释放。该按钮可能由其超级视图保留。

您需要做一些事情来使控制器类在显示按钮的整个过程中保持活动状态(其他一些实例需要保持对它的强引用)。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,但不是参考问题。日志“Interface Builder 文件中的未知类 MyViewController。”对我帮助很大。

    我已经用答案解决了这个问题:Xcode 6 Strange Bug: Unknown class in Interface Builder file

    要解决此问题,请确保正确设置与您的视图关联的模块:

    如果您看到 Module None,则说明存在问题

    进入模块并点击回车即可查看:

    【讨论】:

      【解决方案3】:

      您需要在选择器中添加 : 并更改方法

      - (void)viewDidLoad
      
      {
      
          [super viewDidLoad];
      
          self.title = @"Instruction Controller";
      
          self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] 
              initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self 
              action:@selector(menuButtonClicked:)];   
      
      }
      
      -(void) menuButtonClicked:(id)sender {
          NSLog(@"menuButtonClicked");
      }
      

      【讨论】:

      • 选择器中的 : 只是可选的。如果您添加一个,则该元素会为其自身返回引用,但如果您使用没有 (id)sender 的选择器,它也可以正常工作;)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-28
      • 2012-07-24
      相关资源
      最近更新 更多