继承在 .m 文件中声明的方法 [英] inherit methods declared in .m file

查看:41
本文介绍了继承在 .m 文件中声明的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在知道 Objective-C 中没有受保护的方法,这是我的问题.我有两个具有许多共享功能和属性的 viewController.我的愿景是让 BaseViewController 保存共享的方法和属性,从中两个类将继承和覆盖所需的功能,同时使用相同的变量,我不希望通过将共享函数放在 .h 文件中来将它们转换为公共函数

I now know there is no protected method in Objective-C and here is my problem. I have two viewControllers with many functions and properties that are shared. My vision was to have a BaseViewController holding the shared methods and properties, and from it two classes will inherit and override the needed functionality while using the same variables, I don't wish to convert the shared functions to public by placing them in the .h file

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

To help clarify my question I'm adding code :)

@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' 声明了选择器 'setBtns:'

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

How can I achieve this? Can someone post a snippet or point to my mistake (in code or concept).

推荐答案

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

Just create a second header with the protected methods declared in a category. Name and document the header appropriately.

UIGestureRecognizer.h 和 UIGestureRecognizerSubclass.h 可以为您服务.

UIGestureRecognizer.h and UIGestureRecognizerSubclass.h may server you as an example.

这篇关于继承在 .m 文件中声明的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆