从UIAlertController导航到ViewController按钮单击

问题描述:

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

我将不胜感激任何指针,帮助或解决这个问题。我下面的代码片段给出 -

@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我想在这里做的是。如果“拒绝”按钮被选中,则执行取消操作,并且如果“接受”被选择,则应用应当导航到导航控制器,然后允许我使用应用中的菜单导航到其他ViewController。

早些时候,我用故事板链接一个按钮到NavigationController来遍历我所期望的ViewController。现在我想对AlertController “Accept”按钮进行编程。

预先感谢您。

基本上,要将UIAlertViewController按钮链接到UINavigationController的UIViewController,我们需要在具有U的UIViewController之间创建手动循环IAlertViewController添加到UINavigationController。

请参阅本屏幕截图来看看如何做到以上 -

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) 
} 

这就是处理程序块的用法。你应该在这个块中做出你想要的动作。

编辑:代码

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

你可以使用这个链接作为参考:http://www.appcoda.com/uialertcontroller-swift-closures-enum/

+0

可否请你发布的代码示例代码段只是作为一个例子吗?非常感谢 – 2015-02-10 16:53:18

+1

我编辑了我的答案。 – 2015-02-10 17:03:53

+0

谢谢您的参考。现在我可以做到这一点,我想知道是否可以导航到NavigationController视图控制器,以便它可以使用菜单在应用中进一步导航。 – 2015-02-10 17:28:19

您需要实现处理块来执行代码时选择了行动:

@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) 

} 
+0

那真是太棒了!谢谢! :)现在我如何使用相同的按钮而不是一个viewcontroller导航到NavigationController?其他视图控制器链接到NavigationController。 – 2015-02-10 17:18:01

+0

我编辑了我的答案,如果我正确理解你;) – 2015-02-10 17:43:12

+0

我认为你明白我的意思!我从你的帮助中编辑我的代码,它停止执行,并说“线程1:信号SIGABRT”。任何想法我可能在哪里出错了? – 2015-02-10 17:51:47