2 xinyoulingxi1 tong xinyoulingxi1_tong 于 2013.01.23 10:34 提问

在继承类中调用方法报错

有两个类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
g989_1314125   2013.01.23 13:36
已采纳

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

Mr_me
Mr_me   2013.01.23 10: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:)];
Csdn user default icon
上传中...
上传图片
插入图片