1

我现在知道 Objective-C 中没有受保护的方法,这是我的问题。我有两个具有许多共享功能和属性的视图控制器。我的愿景是拥有一个 BaseViewController 来保存共享方法和属性,并且两个类将继承并覆盖所需的功能,同时使用相同的变量, 我不希望通过将共享函数放在 . h 文件

为了帮助澄清我的问题,我正在添加代码:)

@interface BaseViewController ()
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *uiButtons;
- (void)setBtns:(NSArray *)p_btns; //tried with & without this line
@end

@implementation BaseViewController
- (void)setBtns:(NSArray *)p_btns {
    uiButtons = p_btns;
    //do something generic with the buttons (set font, image etc.)
}
@end

@interface DerivedViewController ()
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *buttonsConnectedToTheActualView;
@end

@implementation DerivedViewController
- (void) setBtns:(NSArray *)p_btns {
    [super setBtns:p_btns];
    //do something specific with the buttons (decide if they face up or down according to this class logic)
}
@end

调用[super setBtns:p_btns];引发错误:

DerivedGameViewController.m:No visible @interface for 'BaseViewController' declares the selector 'setBtns:'

我怎样才能做到这一点?有人可以发布一个片段或指出我的错误(在代码或概念中)。

4

1 回答 1

4

只需使用在类别中声明的受保护方法创建第二个标头。适当地命名和记录标题。

UIGestureRecognizer.h 和UIGestureRecognizerSubclass.h可以作为示例为您服务。

于 2013-03-04T22:31:46.913 回答