从UIAlertController按钮导航到ViewController单击 [英] Navigate to ViewController from UIAlertController button click

查看:46
本文介绍了从UIAlertController按钮导航到ViewController单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am quite new to Swift development and I tried referring to the Swift UIAlertController API but couldn't figure out how to navigate to another UIViewController after clicking a button on the UIAlertController.

对于该问题的任何指导,帮助或解决方案,我将不胜感激.我的代码段如下-

I would appreciate any pointers, help or a solution to this problem. My code snippet is given below -

@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使用应用程序中的菜单.

What I am trying to do here is when a button is clicked the disclaimer AlertController is displayed. If "Decline" button is selected, cancel action is performed and if "Accept" is selected the app should navigate to a Navigation Controller which then allows me to navigate to other ViewControllers using a menu in the application.

之前,我使用故事板将按钮链接到NavigationController,以遍历所需的ViewController.现在,我要以编程方式对AlertController 接受" 按钮执行相同的操作.

Earlier I used the story board to link a button to the NavigationController to traverse to the ViewController I desired. Now I want to do the same programmatically for the AlertController "Accept" Button.

谢谢.

推荐答案

基本上要将UIAlertViewController按钮链接到UINavigationController的UIViewController,我们需要在具有UIAlertViewController的UIViewController和UINavigationController之间创建手动选择.

Basically to link the UIAlertViewController button to a UINavigationController's UIViewController we would need to create a manual segue between the UIViewController which has the UIAlertViewController to the UINavigationController.

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

Please refer to this screen shot to see how to do the above -

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

Then select the link between the UIViewController and the UINavigationController. Go to the left sidebar and select the Attributes inspector and name the identifier.

现在在您的代码中编写以下内容-

Now write the following in your code -

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

这篇关于从UIAlertController按钮导航到ViewController单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆