UIAlertcontroller 作为 Swift 中的一个动作 [英] UIAlertcontroller as an action in Swift

查看:23
本文介绍了UIAlertcontroller 作为 Swift 中的一个动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想弹出一个警报告诉我一条消息,然后我希望它等我直到我按 OK 然后它会继续执行该功能.例如.

So I want to have an alert popping up telling me a message, then I want it to wait for me until i press OK and then it will continue with the function. For example.

@IBAction Alert() {
    let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
    alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(alertController, animated: true, completion: nil)
}

问题是现在它不会做任何事情,如果我只是在之后放入一个函数,它会直接执行它,而不是等到我按下确定".有人知道怎么做吗?

The problem is that now it won't do anything and if I just put in a function afterwards it does it directly, not waiting until I've pressed OK. Does anyone know how to do this?

顺便说一下,在这个例子中,消息是标题,消息,我知道这不是我在这种情况下需要的;)

By the way, in this example the message would be title, message, which I'm aware of and thats not what i need in this case ;)

提前致谢!

推荐答案

您需要将代码放入 OK 按钮处理程序中,而不是将 nil 放在那里.

You need to put your code into the OK button handler, instead of putting nil there.

更改代码:

@IBAction func Alert()
{
   let alertController = UIAlertController(title: title, message: message, preferredStyle:UIAlertControllerStyle.Alert)

   alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default)
   { action -> Void in
     // Put your code here
   })
   self.presentViewController(alertController, animated: true, completion: nil)

}

这篇关于UIAlertcontroller 作为 Swift 中的一个动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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