单击 UIAlertController 按钮导航到 ViewController
Navigate to ViewController from UIAlertController button click
我是 Swift 开发的新手,我尝试参考 Swift UIAlertController API 但不知道如何导航到另一个 UIViewController单击 UIAlertController 上的按钮后。
对于此问题的任何指示、帮助或解决方案,我将不胜感激。我的代码片段如下 -
@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",应用程序应导航到导航然后允许我使用应用程序中的菜单导航到其他 ViewController 的控制器。
之前我使用故事板 link NavigationController 的一个按钮来遍历到我想要的 ViewController。现在我想以编程方式对 AlertController "Accept" 按钮执行相同的操作。
提前致谢。
这就是处理程序块的用途。您应该在此块中进行所需的操作。
编辑:代码
let acceptAction = UIAlertAction(title: "Accept", style: .Default, handler:{ action in
//Write your code here
})
你可以用这个link作为参考:http://www.appcoda.com/uialertcontroller-swift-closures-enum/
您需要实现处理程序块以在选择操作时执行代码:
@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)
}
基本上 link UIAlertViewController 按钮到 UINavigationController 的 UIViewController,我们需要在具有 UIAlertViewController 的 UIViewController 到 UINavigationController 之间创建一个手动连接。
请参阅此屏幕截图以了解如何执行上述操作 -
然后select UIViewController 和UINavigationController 之间的link。转到左侧边栏和 select 属性检查器并命名标识符。
现在在你的代码中写入以下内容 -
@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)
}
我是 Swift 开发的新手,我尝试参考 Swift UIAlertController API 但不知道如何导航到另一个 UIViewController单击 UIAlertController 上的按钮后。
对于此问题的任何指示、帮助或解决方案,我将不胜感激。我的代码片段如下 -
@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",应用程序应导航到导航然后允许我使用应用程序中的菜单导航到其他 ViewController 的控制器。
之前我使用故事板 link NavigationController 的一个按钮来遍历到我想要的 ViewController。现在我想以编程方式对 AlertController "Accept" 按钮执行相同的操作。
提前致谢。
这就是处理程序块的用途。您应该在此块中进行所需的操作。
编辑:代码
let acceptAction = UIAlertAction(title: "Accept", style: .Default, handler:{ action in
//Write your code here
})
你可以用这个link作为参考:http://www.appcoda.com/uialertcontroller-swift-closures-enum/
您需要实现处理程序块以在选择操作时执行代码:
@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)
}
基本上 link UIAlertViewController 按钮到 UINavigationController 的 UIViewController,我们需要在具有 UIAlertViewController 的 UIViewController 到 UINavigationController 之间创建一个手动连接。
请参阅此屏幕截图以了解如何执行上述操作 -
然后select UIViewController 和UINavigationController 之间的link。转到左侧边栏和 select 属性检查器并命名标识符。
现在在你的代码中写入以下内容 -
@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)
}