2016-02-13 51 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) 


}