xinyoulingxi1_tong
2013-01-23 02:34
采纳率: 81.3%
浏览 2.1k
已采纳

在继承类中调用方法报错

有两个类A和B。
在A类中:

@interface A : B

在B类中,得到如下代码:

self.navigationItem.rightBarButtonItem = [UIBarButtonItem barItemWithImage:[UIImage imageNamed:@"Add-button-normal"]
                                                          highlightedImage:[UIImage imageNamed:@"Add-button-active"]
                                                                    target:self
                                                                    action:@selector(showPopoverUI:)];

然后我在A类中访问条目,用下面代码之后没有实现:

self.navigationItem.rightBarButtonItem = [UIBarButtonItem barItemWithImage:[UIImage imageNamed:@"Add-button-normal"]
                                                          highlightedImage:[UIImage imageNamed:@"Add-button-active"]
                                                                    target:self.parentViewController
                                                                    action:@selector(showPopoverUI:)];

然后系统给我错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[B showPopoverUI:]: unrecognized selector sent to class 0x226308'
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • g989_1314125 2013-01-23 05:36
    已采纳

    在第二个例子中你target了parentViewController,应该继续target self。

    点赞 评论
  • Mr_me 2013-01-23 02:55

    你要调用super类的方法,所以需要调用 super

    而这需要调用super navigationItem

    或者:

    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barItemWithImage:[UIImage imageNamed:@"Add-button-normal"]
                                                              highlightedImage:[UIImage imageNamed:@"Add-button-active"]
                                                                        target:self
                                                                        action:@selector(showPopoverUI:)];
    
    点赞 评论

相关推荐 更多相似问题