显示另一个UIWindow的麻烦

我正在写一个iPad应用程序,我正试图在我的应用程序的主窗口顶部显示第二个UIWindow 。 我要做的主要是创建一个登录窗口( 如何使用UISplitViewController呈现登录? ),似乎在这里创建第二个窗口可能是一个不错的选择。

我做了一个非常简单的应用程序试试这个。 当用户点击按钮时,我正在尝试显示第二个窗口。 这是代码:

 - (IBAction)showOtherWindow:(id)sender { UIWindow* otherWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; otherWindow.hidden = NO; otherWindow.clipsToBounds = YES; otherWindow.windowLevel = UIWindowLevelStatusBar; otherWindow.backgroundColor = [UIColor redColor]; [otherWindow makeKeyAndVisible]; } 

我期待在这里看到一个大红色屏幕,但这不会发生 – 没有任何变化。 最后,我希望在顶部有一个较小的窗口。 但是现在我只想看到一个窗口。

如果您使用ARC代码,则在showOtherWindow:返回后,您的窗口将立即被释放。 尝试将otherWindow指定给持久对象中的ivar。

将窗口的指针指定给__strong实例变量(ivar)或强属性。 关闭窗口后,将ivar或属性设置为nil。

在iOS中,您有一个窗口可以填满所有屏幕。 要做你想做的事,你可以创建一个UIViewController / UIView并以模态模式打开它。

在主视图控制器中,您可以执行类似的操作

 UILoginViewController *login = [[UILoginViewController alloc] init]; login.modalPresentationStyle = UIModalPresentationFormSheet; // or whatever you prefer login.completionWithItemsHandler = hdl; [self presentViewController:login animated:YES completion:nil];