【问题标题】:inherit methods declared in .m file继承 .m 文件中声明的方法
【发布时间】:2013-03-04 22:25:36
【问题描述】:

我现在知道 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:'

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

【问题讨论】:

    标签: objective-c uiviewcontroller derived-instances


    【解决方案1】:

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

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

    【讨论】:

    • 我不确定这就是我要找的东西,所以我已经通过添加代码来编辑我的问题,如果你能再看看我会非常感激:)
    猜你喜欢
    • 2023-04-09
    • 2011-04-20
    • 2016-07-20
    • 2019-07-16
    • 1970-01-01
    • 2012-03-20
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多