2016-07-29 200 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)