0

我有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
4

1 回答 1

0

这在直接的 Objective-C 中是不可能的。虽然您可以使用运行时 API 找出 A 的实现viewWillAppear并直接调用它,但这似乎是一种脆弱的方法,通常不建议用于生产代码。

你能详细说明你这样做的理由吗?

于 2013-08-14T11:16:18.793 回答