self.window.rootViewController = self.viewController; // iOS 4+ 才支持
[self.window makeKeyAndVisible];
`UIWindow.rootViewController` 屬性 **iOS 4.0 引入**。在 iOS 3.x 或更早版本運行會導致未識別選擇器崩潰。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// iOS 4+ 使用 rootViewController
if ([self.window respondsToSelector:@selector(setRootViewController:)]) {
self.window.rootViewController = self.viewController;
} else {
// iOS 3 兼容:直接添加子視圖
[self.window addSubview:self.viewController.view];
}
[self.window makeKeyAndVisible];
return YES;
}