我正在尝试使用两个选项创建UIAlertController
'取消'和'退出'。我希望'取消'按钮取消提醒并退出'按钮,用于执行与故障板中设置的segue相关联的segue。
我的代码是;
class HomeVC: UIViewController {
@IBAction func SignOutBtn(sender: UIButton) {
let alertController = UIAlertController(title: "Alert",
message: "Are you sure you want to log out?",
preferredStyle: .Alert)
let cancelAction = UIAlertAction(title:"Cancel",
style: .Cancel) { (action) -> Void in
print("You selected the Cancel action.")
}
let submitAction = UIAlertAction(title:"Log out",
style: .Default) { (action) -> Void in
print("You selected the submit action.")
self.presentedViewController
}
alertController.addAction(submitAction)
alertController.addAction(cancelAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
}
答案 0 :(得分:0)
好吧,您似乎错过了想要在块内执行的操作。 (另外,您也可能想要解除块内的警报控制器。)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action) -> Void in
print("You selected the Cancel action.")
alertController.dismissViewControllerAnimated(true, completion: nil)
})
let submitAction = UIAlertAction(title: "Log out", style: .Default, handler: { (action) -> Void in
print("You selected the submit action.")
alertController.dismissViewControllerAnimated(true, completion: { () -> Void in
// Perform your custom segue action you need.
})
})
答案 1 :(得分:0)
如果你已经从一个视图设置了一个segue,在你的按钮处理程序中注销,那么你可以调用self.performSegueWithIdentifier("storyboadIdentifier")
来调用prepareForSegue
方法,这样你就可以修改或传递信息如果需要,segue。
答案 2 :(得分:0)
@ pbush25
class HomeVC: UIViewController {
@IBAction func SignOutBtn(sender: UIButton) {
let alertController = UIAlertController(title: "Alert",
message: "Are you sure you want to log out?",
preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action) -> Void in
print("You selected the Cancel action.")
alertController.dismissViewControllerAnimated(true, completion: nil)
})
let submitAction = UIAlertAction(title: "Log out", style: .Default, handler: { (action) -> Void in
print("You selected the submit action.")
alertController.dismissViewControllerAnimated(true, completion: { () -> Void in
self.performSegueWithIdentifier("segue", sender: nil)
})
})
alertController.addAction(submitAction)
alertController.addAction(cancelAction)
self.presentViewController(alertController, animated: true, completion: nil)
}