我对 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。如果选择“拒绝”按钮,则执行取消操作;如果选择“接受”,应用程序应导航到导航 Controller ,然后该 Controller 允许我导航到其他 ViewController使用应用程序中的菜单。
之前,我使用 Storyboard将一个按钮链接到 NavigationController,以遍历到我想要的 ViewController。现在我想以编程方式对 AlertController “Accept” 按钮执行相同的操作。
提前谢谢您。
最佳答案
您需要实现处理程序 block 以在选择操作时执行代码:
@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)
}
关于ios - 从 UIAlertController 按钮导航到 ViewController 单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28437104/