presentModalViewController from app delegate

前端 未结 3 2271
借酒劲吻你
借酒劲吻你 2021-02-20 09:49

How can I present a modal view controller from the app delegate\'s view, the top most? Trying to present a modal view controller from a UIView, which made me confused.

3条回答
  •  既然无缘
    2021-02-20 10:06

    The best way to do this is to create a new UIWindow, set it's windowLevel property, and present your UIViewController in that window.

    This is how UIAlertViews work.

    Interface

    @interface MyAppDelegate : NSObject 
    
    @property (nonatomic, strong) UIWindow * alertWindow;
    
    ...
    
    - (void)presentCustomAlert;
    
    @end
    

    Implementation:

    @implementation MyAppDelegate
    
    @synthesize alertWindow = _alertWindow;
    
    ...
    
    - (void)presentCustomAlert
    {
        if (self.alertWindow == nil)
        {
            CGRect screenBounds = [[UIScreen mainScreen] bounds];
            UIWindow * alertWindow = [[UIWindow alloc] initWithFrame:screenBounds];
            alertWindow.windowLevel = UIWindowLevelAlert;
        }
    
        SomeViewController * myAlert = [[SomeViewController alloc] init];
        alertWindow.rootViewController = myAlert;
    
        [alertWindow makeKeyAndVisible];
    }
    
    @end
    

提交回复
热议问题