To avoid that your UIAlertViewController is dismissed by new UIViewController, my solution is:
- Create new UIWindow with level = UIWindowALertLEvel +1
- add empty rootViewController for that UIWindow
- Make that UIWIndow a keyWindow
- show alertController from the rootViewController.
So, this alertcontroller won't not be dismissed by another viewcontroller.
My code:
func showSimpleAlertOverWindow(title: String, msg: String, okButtonTitle : String, animated : Bool) {
CLWrapper.logDebug("show message <\(msg)>")
let _alertWindow = UIWindow(frame: UIScreen.mainScreen().bounds)
_alertWindow.rootViewController = UIViewController()
_alertWindow.windowLevel = UIWindowLevelAlert + 1
_alertWindow.hidden = false
let alert = UIAlertController(title: title ?? "", message: msg ?? "", preferredStyle: UIAlertControllerStyle.Alert)
let okBtn = UIAlertAction(title: okButtonTitle ?? "", style: UIAlertActionStyle.Default) { (alertAction) -> Void in
_alertWindow.resignKeyWindow()
_alertWindow.hidden = true
}
alert.addAction(okBtn)
_alertWindow.makeKeyWindow()
_alertWindow.rootViewController!.presentViewController(alert, animated: animated, completion: nil)
}