单击警报按钮不起作用时更改视图

时间:2016-07-29 11:58:25

标签: ios swift xcode firebase firebase-authentication

美好的一天,

这是我的代码:

@IBAction func logOutAction(sender: AnyObject) {

        try! FIRAuth.auth()!.signOut()
    let alertController = UIAlertController(title: "Sure?", message: "Are you sure you want to log out?", preferredStyle: .Alert)

    let defaultAction = UIAlertAction(title: "Yes", style: .Default, handler: nil)
    alertController.addAction(defaultAction)

    let defaultAction2 = UIAlertAction(title: "No", style: .Cancel, handler: nil)
    alertController.addAction(defaultAction2)

    self.presentViewController(alertController, animated: true, completion: nil)

    let VC = self.storyboard?.instantiateViewControllerWithIdentifier("loginscreen") as! ViewController //The file that controls the view

    self.presentViewController(VC, animated: true, completion: nil)


}

正如您所看到的,我想在按下注销按钮时注销用户。当用户点击注销UIalert弹出窗口询问他们是否确定要注销时,但是当我单击okay时它不会切换回loginscree ViewController。

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:2)

如果最后两行代表您要呈现的视图,那么您必须在"是"的处理程序中执行此代码。按钮动作:

let defaultAction = UIAlertAction(title: "Yes", style: .Default, handler: { _ in
    let VC = self.storyboard?.instantiateViewControllerWithIdentifier("loginscreen") as! ViewController //The file that controls the view

    self.presentViewController(VC, animated: true, completion: nil)
})

alertController.addAction(defaultAction)

答案 1 :(得分:0)

您需要在有人点击“是”

后退出
    let yesActionHandler = { (action:UIAlertAction!) -> Void in
        let VC = self.storyboard?.instantiateViewControllerWithIdentifier("loginscreen") as! ViewController //The file that controls the view
        self.presentViewController(VC, animated: true, completion: nil)
    }
    let defaultAction = UIAlertAction(title: "Yes", style: .Default, handler: yesActionHandler)
    alertController.addAction(defaultAction)