Swift 3.0 中的 UIAlertAction 中的 UIAlertView?
UIAlertView inside UIAlertAction in Swift 3.0 ?
是否可以在 UIAlertAction 中添加 UIAlert 视图?
因为当我尝试在 UIAlertAction 中添加 UIAlert 视图时,它说
"Warning: Attempt to present on whose view is not in the window
hierarchy!"
这是我的代码。
let myAlert = UIAlertController(title: "Title", message: "title here", preferredStyle: UIAlertControllerStyle.alert)
let okaction = UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler:
{
action in
let myViewController:ViewController = self.storyboard!.instantiateViewController(withIdentifier: "ViewController") as! ViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let navigationController = UINavigationController.init(rootViewController: myViewController)
appDelegate.window?.rootViewController = navigationController
appDelegate.window?.makeKeyAndVisible()
if (statement here == 1) {
let myAlert = UIAlertController(title: "Title", message: "title", preferredStyle: UIAlertControllerStyle.alert)
myAlert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil))
self.present(myAlert, animated: true, completion: nil)
return
}
}
)
myAlert.addAction(okaction)
self.present(myAlert, animated: true, completion: nil)
尝试将对第二个警报的调用包装在一个函数中,然后调用该函数。
像这样。
let myAlert = UIAlertController(title: "Title", message: "title here", preferredStyle: UIAlertControllerStyle.alert)
let okaction = UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: { action in
let myViewController:ViewController = self.storyboard!.instantiateViewController(withIdentifier: "ViewController") as! ViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let navigationController = UINavigationController.init(rootViewController: myViewController)
appDelegate.window?.rootViewController = navigationController
appDelegate.window?.makeKeyAndVisible()
if (statement here == 1) {
self.callAlert()
}
})
myAlert.addAction(okaction)
self.present(myAlert, animated: true, completion: nil)
func callAlert() {
let myAlert = UIAlertController(title: "Title", message: "title", preferredStyle: UIAlertControllerStyle.alert)
myAlert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil))
self.present(myAlert, animated: true, completion: nil)
}
尝试在 navigationController
上展示 AlertController
。
换行
self.present(myAlert, animated: true, completion: nil)
与
navigationController.present(myAlert, animated: true, completion: nil)
是否可以在 UIAlertAction 中添加 UIAlert 视图?
因为当我尝试在 UIAlertAction 中添加 UIAlert 视图时,它说
"Warning: Attempt to present on whose view is not in the window hierarchy!"
这是我的代码。
let myAlert = UIAlertController(title: "Title", message: "title here", preferredStyle: UIAlertControllerStyle.alert)
let okaction = UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler:
{
action in
let myViewController:ViewController = self.storyboard!.instantiateViewController(withIdentifier: "ViewController") as! ViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let navigationController = UINavigationController.init(rootViewController: myViewController)
appDelegate.window?.rootViewController = navigationController
appDelegate.window?.makeKeyAndVisible()
if (statement here == 1) {
let myAlert = UIAlertController(title: "Title", message: "title", preferredStyle: UIAlertControllerStyle.alert)
myAlert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil))
self.present(myAlert, animated: true, completion: nil)
return
}
}
)
myAlert.addAction(okaction)
self.present(myAlert, animated: true, completion: nil)
尝试将对第二个警报的调用包装在一个函数中,然后调用该函数。
像这样。
let myAlert = UIAlertController(title: "Title", message: "title here", preferredStyle: UIAlertControllerStyle.alert)
let okaction = UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: { action in
let myViewController:ViewController = self.storyboard!.instantiateViewController(withIdentifier: "ViewController") as! ViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let navigationController = UINavigationController.init(rootViewController: myViewController)
appDelegate.window?.rootViewController = navigationController
appDelegate.window?.makeKeyAndVisible()
if (statement here == 1) {
self.callAlert()
}
})
myAlert.addAction(okaction)
self.present(myAlert, animated: true, completion: nil)
func callAlert() {
let myAlert = UIAlertController(title: "Title", message: "title", preferredStyle: UIAlertControllerStyle.alert)
myAlert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil))
self.present(myAlert, animated: true, completion: nil)
}
尝试在 navigationController
上展示 AlertController
。
换行
self.present(myAlert, animated: true, completion: nil)
与
navigationController.present(myAlert, animated: true, completion: nil)