0

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

3 回答 3

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:)];
于 2013-01-22T17:23:01.493 回答
1

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

尝试:

self.navigationItem.rightBarButtonItem = [UIBarButtonItem barItemWithImage:[UIImage imageNamed:@"Add-button-normal"] highlightedImage:[UIImage imageNamed:@"Add-button-active"]target:self action:@selector(showPopoverUI:)];
于 2013-01-22T17:24:37.947 回答
1

您在第二个示例中以 parentViewController 为目标,而应继续以 self 为目标。

于 2013-01-22T18:00:39.683 回答