为什么我的应用程序中有两个UIWindows,为什么隐藏第一个UIWindows?

时间:2015-04-04 16:10:34

标签: ios cocoa-touch uiwindow

嗯,实际上是3,但其中一个是UITextEffectsWindow

所以我开始了一个简单的测试应用程序。

的AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    ViewController *controller = [[ViewController alloc] init];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
    self.window.rootViewController = navController;
    [self.window makeKeyAndVisible];
}

在viewController中,我正在标记视图:

    self.view.tag = 999;

当我退出此

    NSLog(@"[UIApplication sharedApplication].windows = %@",[UIApplication sharedApplication].windows);
    for (UIWindow* window in [UIApplication sharedApplication].windows) {
        NSLog(@"tag = %d",window.rootViewController.view.tag);
    }

我明白了:

2015-04-04 08:59:44.440 SDK[45888:1495552] [UIApplication sharedApplication].windows = (
    "<UIWindow: 0x7fc57371e1e0; frame = (0 0; 375 667); hidden = YES; gestureRecognizers = <NSArray: 0x7fc57371eb10>; layer = <UIWindowLayer: 0x7fc57371c2f0>>",
    "<UIWindow: 0x7fc57352b0d0; frame = (0 0; 375 667); gestureRecognizers = <NSArray: 0x7fc5750115f0>; layer = <UIWindowLayer: 0x7fc573526f20>>",
    "<UITextEffectsWindow: 0x7fc5750260c0; frame = (0 0; 375 667); opaque = NO; gestureRecognizers = <NSArray: 0x7fc575026fb0>; layer = <UIWindowLayer: 0x7fc575026580>>"
)
2015-04-04 09:03:33.319 SDK[46643:1499077] tag = 999
2015-04-04 09:03:33.319 SDK[46643:1499077] tag = 0
2015-04-04 09:08:11.648 SDKVevo[47379:1501367] key
2015-04-04 09:03:33.319 SDK[46643:1499077] tag = 0

如您所见,有两个UIWindows,第一个是隐藏的。而且,第一个的标签是999,因此VC实例化并设置为root。但是那个窗口怎么标记为隐藏?为什么首先有两个?为什么第二个窗口注销为keyWindow?

当我运行应用程序时,我实际上看到VC我实例化并标记为999,但日志说它是非keyWindow的一部分,它也是一个隐藏窗口。

1 个答案:

答案 0 :(得分:2)

问题是你正在创建一个窗口:

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

...但是你忘了从创建一个窗口来停止故事板!如果您要像这样手动创建窗口,则应删除故事板并设置 Info.plist ,以使其没有主故事板设置。你不能两者手动创建一个窗口使用故事板 - 你在应用程序的结构中造成了内部冲突。