Ios 为什么即使视图控制器未显式添加为子级,也会调用外观方法?

Ios 为什么即使视图控制器未显式添加为子级,也会调用外观方法?,ios,uiviewcontroller,Ios,Uiviewcontroller,在研究容器视图控制器试图重构一些代码时,我发现了一些我不理解的东西 Apple文档告诉我,为了让子视图控制器调用其外观方法,必须使用addChildViewController: @interface ChildViewController : UIViewController @end @implementation ChildViewController - (void)loadView { self.view = [[UIView alloc] initWithFrame:CG

在研究容器视图控制器试图重构一些代码时,我发现了一些我不理解的东西

Apple文档告诉我,为了让子视图控制器调用其外观方法,必须使用
addChildViewController:

@interface ChildViewController : UIViewController
@end

@implementation ChildViewController

- (void)loadView {
    self.view = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 250.0f, 250.0f)];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"ChildViewController:viewWillAppear:");
}

@end


@interface RootViewController : UIViewController
@property (strong) ChildViewController *cvc;
@end

@implementation RootViewController
@synthesize cvc;
- (void)loadView {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 500.0f, 500.0f)];
    cvc = [[ChildViewController alloc] init];
    [view addSubview:[cvc view]];
    self.view = view;
}

@end


@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = [[RootViewController alloc] init];
    [self.window makeKeyAndVisible];

    return YES;
}

@end
这让我感到困惑,因为我的代码没有使用任何容器视图控制器方法,但我的所有子视图控制器都获得了
视图将出现:
消息

我将代码归结为这个简单的示例,您将在调试日志中看到“ChildViewController:ViewWillDisplay:”,尽管调用了
addChildViewController:

@interface ChildViewController : UIViewController
@end

@implementation ChildViewController

- (void)loadView {
    self.view = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 250.0f, 250.0f)];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"ChildViewController:viewWillAppear:");
}

@end


@interface RootViewController : UIViewController
@property (strong) ChildViewController *cvc;
@end

@implementation RootViewController
@synthesize cvc;
- (void)loadView {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 500.0f, 500.0f)];
    cvc = [[ChildViewController alloc] init];
    [view addSubview:[cvc view]];
    self.view = view;
}

@end


@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = [[RootViewController alloc] init];
    [self.window makeKeyAndVisible];

    return YES;
}

@end

为什么这样做?

调用
addSubview
的过程将导致视图出现,从而导致
loadView
viewDidLoad
viewdisped
viewdisped
等调用。
addChildViewController
(以及您也应该执行的对
didMoveToParentViewController
的调用)不会影响此操作

您可以调用
addChildViewController
,以确保控制器层次结构与视图层次结构保持同步。如果不这样做,则无法将某些事件传递给子控制器(例如旋转事件)。另外,通过执行
addChildViewController
,您的控制器将为您保留,而无需维护自己的属性来跟踪子控制器


如果你看到了,它将讨论保持视图层次结构和控制器层次结构同步的重要性。

你能链接到哪个苹果文档中说的吗?也许我只是很笨,但我找不到它。