Ios 当用户单击“确定”时,如何显示UIAlertController并执行操作?
我被Ios 当用户单击“确定”时,如何显示UIAlertController并执行操作?,ios,swift,uialertcontroller,Ios,Swift,Uialertcontroller,我被UIAlertController卡住了 我有一个警告视图: @IBAction func AddPatientButton(sender: AnyObject) { let alert = UIAlertController(title:"Registro Paciente", message: "Paciente Registrado", preferredStyle: UIAlertControllerStyle.Alert) alert.addAction(UIAl
UIAlertController
卡住了
我有一个警告视图:
@IBAction func AddPatientButton(sender: AnyObject) {
let alert = UIAlertController(title:"Registro Paciente", message: "Paciente Registrado", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title:"Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
它正确地向我显示了警报视图。但我想要的是,当用户按下“确定”按钮时,此按钮执行一个序列并将用户发送到另一个ViewController。此示例代码满足您的要求
@IBAction func AddPatientButton(sender: AnyObject) {
let alert = UIAlertController(title:"Registro Paciente", message: "Paciente Registrado", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title:"Ok", style: UIAlertActionStyle.Default, handler: { alertAction in
// Code to segue/change VC !
self.performSegueWithIdentifier("segue_name", animated: true)
//
}))
只需确保为要转到的视图控制器创建一个segue,并在inspector中为该segue指定一个标识符(单击segue并检查inspector)。然后将该标识符复制粘贴到“segue_name”所在的位置
让我用图片展示一下:
1) 添加一个新的VC(除非你已经有了一个你想去的VC)。然后按住Ctrl键,将来自的VC的黄色图标拖动到要转到的VC:
2) 选择“显示”或您要查找的任何选项:
3) 选择序列(出现的箭头),单击属性检查器(右上角),然后填写标识符:
然后复制粘贴我给你的代码中“segue_name”所在的标识符。你需要向按钮添加一个处理程序。你就快到了。向要转到的视图控制器添加手动分段。确保向该序列添加标识符
alert.addAction(UIAlertAction(title:"Ok", style: UIAlertActionStyle.Default, handler: {
self.performSegueWithIdentifier("my_segue_name", animated: true)
}))
您需要使用
UIAlertAction
上的handler
参数。不要关闭警报操作处理程序中的警报控制器。警报已被解除。感谢您的帮助和时间,非常详细和准确:)为什么调用disatch\u async
?我想我需要它。因为它是对按钮按下的直接反应,所以不需要它是有道理的。
let alertController = UIAlertController(title: titleAlert, message: msg, preferredStyle: UIAlertControllerStyle.Alert)
let go = UIAlertAction(title: titleBtn, style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) in
//Your Action here
})
alertController.addAction(go)
alertController.addAction(UIAlertAction(title: "CANCEL", style: UIAlertActionStyle.Destructive, handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)