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.
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 UIAlertView
s 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