从继承类调用方法

时间:2013-01-22 17:17:41

标签: iphone ios

我有两个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'

3 个答案:

答案 0 :(得分:1)

如果您想调用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:)];

答案 1 :(得分:1)

如果你继承你应该只能使用self并调用方法就好像它是你自己的

尝试:

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

答案 2 :(得分:1)

您在第二个示例中定位了parentViewController,而不是继续定位self。