swift - UIAlertController 错误

标签 swift

我从我的添加栏按钮项中调用以下代码,我在其中显示警报 View 以请求用户输入。它第一次运行良好,之后出现以下错误:

代码:

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)

    }

Error : 'UIAlertController can only have one action with a style of UIAlertActionStyleCancel'

我认为每次按下添加按钮时它都会尝试添加操作,因此会出现错误。如果我错了,请纠正我,也请提出解决方法。

感谢您的帮助。

最佳答案

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

    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在第二个

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

工作正常

关于swift - UIAlertController 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29592522/

相关文章:

ios - 手动添加库到 Xcode 项目

Swift 5.5,何时在自定义异步实现中使用 `Task.suspend`?

ios - 如何在 xcode 中解析和分类来自 AlamoFire 的 JSON 响应

ios - 使用导航 Controller 在 Storyboard 中呈现 View Controller - Swift

ios - 将两个不同类型的数组排序为一个

ios - 如何在 iOS 快速模块化应用程序中执行路由?

ios - 如何创建模板化的可滑动 View ?

ios - 当我向左或向右移动操纵杆时,如何更改节点的纹理?

swift - ARKit:将 SCNText 放置在相机前面的特定点

ios - 可以从 iOS 中获取来电号码并显示来电者姓名