2016-07-29 158 views
1

好日子,更改视图

这是我的代码:

@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弹出窗口询问他们是否确定要注销时,但是当我点击好的时候,它不会切换回loginscree ViewController。

任何人都可以帮助我吗?

回答

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) 
+0

http://imgur.com/a/1mKQp我得到这个错误。我究竟做错了什么? – matthewdossantos

+0

对不起,我的代码中存在拼写错误。我已经更新了 – fiks

+0

谢谢!完美的作品。在2分钟内接受你的答案。 – matthewdossantos

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)