Вариант A: реализовать метод в категории
Все используемые свойства должны быть объявлены в UIViewController
.
UITableViewController
является подклассом UIViewController
.
//UIViewController+MyAdditions.h
@interface UIViewController (MyAdditions)
- (void)myCommonMethod;
@end
//UIViewController+MyAdditions.m
@implementation UIViewController (MyAddtions)
- (void)myCommonMethod {
// insert code here
}
Новый метод, добавленный к UIViewController
, будет унаследован Bar1
и Bar2
Вариант B: Создать MyViewControllerHelper
класс
Если вы можете реализовать свой общий код как метод класса, в противном случае вам потребуется создать экземпляр вашего вспомогательного класса либо временно, либо как свойство Bar1
и Bar2
@interface MyViewControllerHelper : NSObject
- (void)myCommonMethod;
@end
@implementation MyViewControllerHelper
- (void)myCommonMethod {
// common code here
}
@interface Bar1 : UIViewController {
MyViewControllerHelper *helper;
}
@property MyViewControllerHelper *helper;
@end
@implementation Bar1
@synthesize helper;
- (void)someMethod {
[helper myCommonMethod];
}
@end
@interface Bar2 : UITableViewController {
MyViewControllerHelper *helper;
}
@property MyViewControllerHelper;
@end
@implementation Bar2
@synthesize helper;
- (void)someOtherMethod {
[helper myCommonMethod];
}
@end