-->

inherit methods declared in .m file

2019-06-02 16:39发布

问题:

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 BaseViewColntroller holding the share 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

the call to [super setBtns:p_btns]; raise an error: DerivedGameViewController.m:No visible @interface for 'BaseViewController' declares the selector 'setBtns:'

how can i achieve this? can someone post a snippet or point to my mistake (in code or concept)

Thanks

回答1:

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

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