在研究容器视图控制器以尝试重构一些代码时,我遇到了一些我不理解的东西。
Apple 文档告诉我,为了让子视图控制器调用它们的外观方法,它们必须作为子视图控制器添加到父视图控制器中addChildViewController:
这让我感到困惑,因为我的代码没有使用任何容器视图控制器方法,但我所有的子视图控制器都收到了viewWillAppear:
消息。
我将代码简化为这个简单的示例,尽管调用了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
为什么这行得通?