2016-02-13 54 views
0

我想創建一個UIAlertController有兩個選項'取消'和'註銷'。我想要'取消'按鈕來取消警報和'註銷'按鈕來執行與它相關的繼續,這是我在故事板中設置的。UIAlertController - 過渡到另一個賽格

我的代碼是;

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

@Rohit KP我無法Segue公司回到我的VC,如果我選擇 「退出」 – SwiftBeginner

回答

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

感謝。我如何以編程方式執行segue動作?我用ctrl +拖動它到故事板上的VC,我怎麼稱呼它? – SwiftBeginner

+0

謝謝。我如何以編程方式執行segue動作?我用ctrl +拖動它到故事板上的VC,我怎麼稱呼它? - 我試圖添加self.performSegueWithIdentifier(「segue」,發件人:無),但無論按下哪個按鈕,都讓我處於相同的VC – SwiftBeginner

+0

請參閱下面的@ pbush25答案。 – Lirik

0

如果你已經從一個視圖建立一個SEGUE到另一個,在記錄您的按鈕處理了,那麼你可以請致電self.performSegueWithIdentifier("storyboadIdentifier"),這將調用prepareForSegue方法,以便您可以根據需要修改或傳遞信息。

+0

請參閱下面的答案以獲得更新的代碼。 Segue仍然沒有被調用。 – SwiftBeginner

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) 


}