UIAlertController按钮单击导航到NavigationController的第一个ViewController

时间:2015-02-23 18:22:32

标签: swift uinavigationcontroller xcode6 viewcontroller uialertcontroller

我是swift和iOS编程的新手。我有 StartViewController ,它有一个按钮,点击一个带有两个按钮的 UIAlertController - 拒绝& 接受的。单击 Accept 时,我想导航到 MyNavigationController 的 - ViewController MyNavigationController 有四个ViewControllers,我使用幻灯片菜单导航到它。

我附上了我的故事板的示例代码和屏幕截图供参考。

 @IBAction func showAlert() {

    let alertController = UIAlertController(title: "Disclaimer", message: "Before using this teaching resource, you confirm that you agree:\n1. To obey the law regarding data protection and patient confidentiality.\n2. To us this app professionally and appropriately in clinical settings.\n3. This is for your personal use and you may not modify, distribute, publish, transfer any information obtained from this teaching resource without the developers' permission.\n4. In no event shall the developer be liable to you for any loss arising from your use of this resource.", preferredStyle: .Alert)


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

    let acceptAction = UIAlertAction(title: "Accept", style: .Default) { (_) -> Void in

             //I think the code to navigate should go here, help please.

    }

    alertController.addAction(acceptAction)

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

}

故事板截图 -

enter image description here

在上面的屏幕截图中,登录按钮打开了一个UIAlertController alertController 。此AlertController上的接受按钮应导航到 MyNavigationController 上的 ViewController

MyNavigationController 有三个其他ViewControllers,使用幻灯片菜单进行导航。

提前谢谢。

1 个答案:

答案 0 :(得分:1)

您需要在当前视图控制器和目标视图控制器之间创建一个segue,并确保segue ID与performSegueWithIdentifier调用中的ID匹配

@IBAction func showAlert() {

    let alertController = UIAlertController(title: "Disclaimer", message: "Before using this teaching resource, you confirm that you agree:\n1. To obey the law regarding data protection and patient confidentiality.\n2. To us this app professionally and appropriately in clinical settings.\n3. This is for your personal use and you may not modify, distribute, publish, transfer any information obtained from this teaching resource without the developers' permission.\n4. In no event shall the developer be liable to you for any loss arising from your use of this resource.", preferredStyle: .Alert)

    let declineAction = UIAlertAction(title: "Decline", style: .Cancel, handler: nil)
    let acceptAction = UIAlertAction(title: "Accept", style: .Default) { (_) -> Void in    

        self.performSegueWithIdentifier("SomeSegue", sender: self) // Replace SomeSegue with your segue identifier
    }

    alertController.addAction(declineAction)
    alertController.addAction(acceptAction)

    presentViewController(alertController, animated: true, completion: nil)
}