Swift3 Swift 3.0中UIAlertAction内部的UIAlertView?
是否可以在UIAlertAction中添加UIAlert视图 因为当我试图在UIAlertAction中添加UIAlert视图时,它说 “警告:试图显示其视图不在窗口中的对象。” 等级制度!” 这是我的密码Swift3 Swift 3.0中UIAlertAction内部的UIAlertView?,swift3,appdelegate,uialertcontroller,uialertaction,Swift3,Appdelegate,Uialertcontroller,Uialertaction,是否可以在UIAlertAction中添加UIAlert视图 因为当我试图在UIAlertAction中添加UIAlert视图时,它说 “警告:试图显示其视图不在窗口中的对象。” 等级制度!” 这是我的密码 let myAlert = UIAlertController(title: "Title", message: "title here", preferredStyle: UIAlertControllerStyle.alert) let okaction = UIAlertAction(
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)
假设我上面的代码在SecondViewController中,如果(statement==1){}正在使用上面的代码使其工作。但是,如果我在它下面有另一个“if(statement==2){}”,并且我想显示警报而不去rootViewController该怎么办呢。我的意思是它可以留在SecondViewController中吗?@AlotJai然后你需要写这两行
appDelegate.window?.rootViewController=navigationController appDelegate.window?.makeKeyAndVisible()
insideif(statement==1){}