从 UIAlertController 按钮单击导航到 ViewController

Posted

技术标签:

【中文标题】从 UIAlertController 按钮单击导航到 ViewController【英文标题】:Navigate to ViewController from UIAlertController button click 【发布时间】:2015-02-10 16:45:42 【问题描述】:

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

我将不胜感激任何指针、帮助或解决此问题的方法。我的代码 sn-p 如下 -

@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。如果选择了 "Decline" 按钮,则会执行取消操作,如果选择了 "Accept",应用程序应该导航到导航控制器,然后我可以导航到其他视图控制器使用应用程序中的菜单。

之前我使用故事板将一个按钮链接到 NavigationController 以遍历我想要的 ViewController。现在我想以编程方式对 AlertController "Accept" 按钮执行相同的操作。

提前谢谢你。

【问题讨论】:

【参考方案1】:

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

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


【讨论】:

你太棒了!谢谢! :) 现在我如何使用同一个按钮而不是一个视图控制器导航到 NavigationController?其他视图控制器链接到 NavigationController。 如果我理解正确,我已经编辑了我的答案;) 我想你已经明白我的意思了!我在您的帮助下编辑了我的代码,它停止执行并显示“线程 1:信号 SIGABRT”。知道我哪里出错了吗? 您应该更明确地说明错误:您是否有任何崩溃的断点或更多信息?您应该使用新添加的代码编辑您的问题。 我已经编辑了我的问题和代码。我感谢所有帮助 Michaël :)【参考方案2】:

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

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

然后选择 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)

【讨论】:

【参考方案3】:

这就是处理程序块的用途。您应该在此块中进行所需的操作。

编辑:代码

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

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

【讨论】:

能否请您发布一个示例 sn-p 代码作为示例?非常感谢 感谢您的参考。现在我可以做到这一点,我想知道我是否可以导航到 NavigationController 视图控制器,以便它允许我使用菜单在应用程序中进一步导航。 我不懂使用菜单。 我有一个带有警报控制器的 StartViewController。现在,当我单击接受时,我想导航到一个导航控制器堆栈(这将帮助我使用滑动菜单栏遍历我的应用程序的其他部分)。所以我想知道如何导航到具有 ViewController1 的导航控制器堆栈。 啊哈,类似于你在情节提要中制作的 push segue?

以上是关于从 UIAlertController 按钮单击导航到 ViewController的主要内容,如果未能解决你的问题,请参考以下文章

如何在 UIAlertController 上添加动态按钮?

在 UIAlertController Action 中关闭 UIViewController 会导致崩溃

TextfieldShouldBeginediting Objective-c 中的 UIAlertcontroller

呈现的 UIViewController 不能呈现 UIAlertController

使用 StoreKit 时获取 Apple UIAlertController 完成

从 UINavigationItem 扩展中呈现 UIAlertController