trouble with displaying another UIWindow

柔情痞子 提交于 2019-12-06 08:13:04

问题


I'm writing an iPad app, and I'm trying to display a second UIWindow on top of the main window in my app. The main thing I'm trying to do is create a log in window (how to present a login, with UISplitViewController?), and it seems that creating a second window here might be a good option.

I've made a very simple app to try this out. When the user hits a button, then I'm trying to show the second window. Here's the code:

- (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];
}

I'm expecting to see a big red screen here, but that doesn't happen - nothing changes. Ultimately, I'd like to have a smaller window floating on top. But right now I'd just like to see a window at all.


回答1:


If you're in ARC code your window is getting deallocated immediately after showOtherWindow: returns. Try assigning otherWindow to an ivar in a persistent object.




回答2:


Assign the window's pointer to a __strong instance variable (ivar) or a strong property. Set the ivar or property to nil after you dismiss the window.




回答3:


In iOS you have one window that fills all the screen. To do what you want you may create a UIViewController/UIView and open it in modal mode.

From your main View Controller you may do something like

UILoginViewController *login = [[UILoginViewController alloc] init]; 
login.modalPresentationStyle = UIModalPresentationFormSheet; // or whatever you prefer

login.completionWithItemsHandler = hdl;

[self presentViewController:login animated:YES completion:nil];


来源:https://stackoverflow.com/questions/15909017/trouble-with-displaying-another-uiwindow

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!