我正在尝试创建具有两个选项“取消”和“注销”的UIAlertController。我希望“取消”按钮取消警报,并希望“注销”按钮执行与其关联的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)
}

}

最佳答案

好吧,似乎您错过了要在块内执行的操作。
(此外,您可能还希望在块内关闭警报控制器。)

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.
    })
})

09-26 01:07