调用特定的超类

时间:2013-08-14 11:08:33

标签: objective-c

我有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] BViewController.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

但是需要的是:

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

1 个答案:

答案 0 :(得分:0)

直接Objective-C无法做到这一点。虽然您可以使用运行时API来确定A viewWillAppear的实现并直接调用它,但这似乎是一种脆弱的方法,通常不建议用于生产代码。

你能详细说明一下你的理由吗?