首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIAlertController错误

UIAlertController错误
EN

Stack Overflow用户
提问于 2015-04-12 17:45:02
回答 3查看 4.8K关注 0票数 3

我从我的添加栏按钮项目调用下面的代码,在这里我显示警告视图,询问用户的输入。它第一次运行良好,并在此之后给出了以下错误:

代码:

代码语言:javascript
复制
var alert = UIAlertController(title: "Enter Blog Link", message: nil, preferredStyle: .Alert)
    func userBlogLinkEntryPopover() {
        //        let alert = UIAlertView(title: "Enter Blog Link", message: nil, delegate: self, cancelButtonTitle: "Cancel")
        alert.addTextFieldWithConfigurationHandler { (textField) -> Void in
            textField.placeholder = "Enter Blog URL!"
        }
        alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in
            if let tf = self.alert.textFields?.first as? UITextField{
                println(tf.text)
            }
        }))
        alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
        presentViewController(alert, animated: true, completion: nil)

    }

错误:“UIAlertController只能有一个具有UIAlertActionStyleCancel样式的操作”

我认为,每次按下“添加”按钮时,它都会尝试添加操作,从而导致错误。如果我错了,请纠正我,也请指点周围的工作。

谢谢你的帮助。

EN

回答 3

Stack Overflow用户

发布于 2017-11-05 00:07:32

如果你这样做,你也会得到这个,仔细阅读,看看你是否能看到错误。

代码语言:javascript
复制
    resendAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
        //cancel clicked
        return
    }))

    resendAlert.addAction(UIAlertAction(title: "Check Server", style: .cancel, handler: { (action: UIAlertAction!) in
        //check server clicked
        return
    }))

注意-> .cancel而不是,要使用两次,第二次需要是.default

代码语言:javascript
复制
    resendAlert.addAction(UIAlertAction(title: "Check Server", style: .default, handler: { (action: UIAlertAction!) in
        //check server clicked
        return
    }))

工作很好

票数 13
EN

Stack Overflow用户

发布于 2015-04-12 17:56:45

我发现,由于在函数之外声明了警报,所以它保留了所有操作,并因此引发异常。我更正了我的代码如下,它工作得很好。

代码语言:javascript
复制
 func userBlogLinkEntryPopover() {       
        var alert = UIAlertController(title: "Enter Blog Link", message: nil, preferredStyle: .Alert)
        alert.addTextFieldWithConfigurationHandler { (textField) -> Void in
            textField.placeholder = "Enter Blog URL!"
        }
        alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in
            if let tf = alert.textFields?.first as? UITextField{
                println(tf.text)
            }
        }))
        alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
        presentViewController(alert, animated: true, completion: nil)

    }

问候

票数 3
EN

Stack Overflow用户

发布于 2018-08-14 14:13:27

这是我被绊倒的原因-一个自动填充的错误。使用ObjC,而不是在操作中使用样式UIAlertActionStyleDefault,而是使用UIAlertControllerStyleAlert常量。没有警告,但这两种解析的枚举值为1,因此我无意中向同一个控制器添加了两个取消操作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29592522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档