Objective c 调用特定超类
我有Objective c 调用特定超类,objective-c,Objective C,我有AViewController.h如下: @interface AViewController : UIViewController @end @interface BViewController : AViewController; - (void)MethodB; @end @interface CViewController : BViewController; @end 然后我有AViewController.m如下 @implementation AViewControl
AViewController.h
如下:
@interface AViewController : UIViewController
@end
@interface BViewController : AViewController;
- (void)MethodB;
@end
@interface CViewController : BViewController;
@end
然后我有AViewController.m
如下
@implementation AViewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"AViewController.viewWillAppear");
}
@end
@implementation BViewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"BViewController.viewWillAppear");
}
- (void)MethodB
{
NSLog(@"show MehtodB");
}
@end
@implementation CViewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated]; <--*** I want this line to call AViewController.viewWillAppear directly ***
NSLog(@"CViewController.viewWillAppear");
[self MethodB];
}
但需要的是:
2013-08-14 17:37:05.205 P1[12876:c07] AViewController.viewWillAppear
2013-08-14 17:37:05.206 P1[12876:c07] CViewController.viewWillAppear
2013-08-14 17:37:05.207 P1[12876:c07] show MehtodB
这在straight Objective-C中是不可能的。虽然您可以使用运行时API找出A的
视图的实现,并直接调用它,但这似乎是一种脆弱的方法,通常不建议用于生产代码
您能详细说明一下这样做的原因吗?为什么要跳过B,为什么需要这个继承链?如果需要这样做,您的惯性中有一个设计缺陷hierarchy@giorashc,因为我想在不同的位置使用BViewController和CViewController,例如,如果我创建XViewController,在XViewController中,有两个按钮,第一个按钮,当你按下它后,BViewController将被打开,另一个按钮,当你按下它时,它将打开CViewController。你能详细说明一下这种常用方法的作用吗?我认为您应该重新考虑您的设计,因为视图控制器不应该是复杂的继承。我这样做的原因是AViewController有一组BViewController需要的方法,所以我不需要再次重写,然后我指定BViewController继承AViewController,这样我就可以使用AViewController中所需的一组方法。对于CViewController,我也使用了上面描述的相同逻辑,因为我需要使用BViewController中的一些方法和AViewController中的所有方法。关于如何以不同的方式实现此问题,您有什么建议吗?哪种方法更好?
2013-08-14 17:37:05.205 P1[12876:c07] AViewController.viewWillAppear
2013-08-14 17:37:05.206 P1[12876:c07] CViewController.viewWillAppear
2013-08-14 17:37:05.207 P1[12876:c07] show MehtodB