从UIAlertController按钮单击导航到ViewController

时间:2015-02-10 16:45:43

标签: ios swift uiviewcontroller xcode6 uialertcontroller

我对Swift开发很新,我尝试引用Swift UIAlertController API但是在点击UIAlertController上的按钮之后无法弄清楚如何导航到另一个UIViewController。

我很感激任何指针,帮助或解决此问题。我的代码片段如下 -

@IBAction func showAlert() {

    let alertController = UIAlertController(title: "Disclaimer", message: "Disclaimer Text Here", preferredStyle: .Alert)


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

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

        let secondVC = ViewController(nibName: "ViewController", bundle: nil)
        let navController = UINavigationController(rootViewController: secondVC)
        self.presentViewController(navController, animated: true, completion: nil)

    }
    alertController.addAction(acceptAction)

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

}

我在这里要做的是当点击一个按钮时,显示免责声明AlertController。如果选择了"拒绝" 按钮,则会执行取消操作,如果选择"接受" ,应用应导航到导航控制器然后允许我使用应用程序中的菜单导航到其他ViewControllers。

之前我使用故事板将一个按钮链接到NavigationController以遍历我想要的ViewController。现在我想以编程方式为AlertController "接受" 按钮做同样的事情。

提前谢谢。

3 个答案:

答案 0 :(得分:2)

您需要实现处理程序块以在选择操作时执行代码:

@IBActionfunc showAlert() {

    let alertController = UIAlertController(title: "Disclaimer", message: "Disclaimer Text Here", preferredStyle: .Alert)


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

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

        let secondVC = SecondViewController(nibName: "SecondView", bundle: nil)
        let navController = UINavigationController(rootViewController: secondVC)
        self.presentViewController(navController, animated: true, completion: nil)
    }
    alertController.addAction(acceptAction)

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

}

答案 1 :(得分:2)

基本上要将UIAlertViewController按钮链接到UINavigationController的UIViewController,我们需要在UIViewController和UINavigationController之间创建一个手动segue。

请参阅此屏幕截图,了解如何进行上述操作 -

enter image description here

然后选择UIViewController和UINavigationController之间的链接。转到左侧边栏并选择“属性”检查器并命名标识符。

现在在代码中写下以下代码 -

@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 (name)
}

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

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

答案 2 :(得分:1)

这是处理程序块的使用。你应该在这个街区做出你想要的行动。

编辑:代码

let acceptAction = UIAlertAction(title: "Accept", style: .Default, handler:{  action in 
    //Write your code here
})

您可以将此链接用作参考:http://www.appcoda.com/uialertcontroller-swift-closures-enum/