ios - 快速代码返回警报类型字符串不起作用

标签 ios swift

我发现无效返回警报但未找到字符串警报返回。 尝试 Void 到 String

// return Void Alert
func returnAlert(title: String!, message: String! ,success: (() -> Void)? , cancel: (() -> Void)?) {
    dispatch_async(dispatch_get_main_queue(), {
        let alertController = UIAlertController(title:title,
            message: "",
            preferredStyle: UIAlertControllerStyle.Alert)

        self.newQtyField = UITextField()
        self.newQtyField.keyboardType = .NumberPad

        func addTextField(textField: UITextField!){
            // add the text field and make the result global
            let row = self.array[Int(message)!]
            textField.text = String(row.pbQty!)
            self.newQtyField = textField
        }


        let cancelLocalized = NSLocalizedString("cancel", tableName: "activity", comment:"")
        let okLocalized = NSLocalizedString("ok", tableName: "Localizable", comment:"")

        let cancelAction: UIAlertAction = UIAlertAction(title: cancelLocalized,
        style: .Cancel) {
            action -> Void in cancel?()
        }
        let successAction: UIAlertAction = UIAlertAction(title: okLocalized,
        style: .Default) {
            action -> Void in success?()
        }
        alertController.addTextFieldWithConfigurationHandler(addTextField)
        alertController.addAction(cancelAction)
        alertController.addAction(successAction)


        self.presentViewController(alertController, animated: true, completion: nil)
    })
}

//底部调用乐趣 |我认为 success: { () to success: { textfield ,但无法正常工作......需要你的帮助

    returnAlert("title", message: "msg", success: { () -> Void in


        })
    { () -> Void in
        print("user canceled")
    }

最佳答案

很难理解您在问题中所说的内容。

您应该将方法定义更改为:

func returnAlert(title: String!, message: String! ,success: ((text:String) -> Void)? , cancel: (() -> Void)?) {

并更改 successAction 以返回 UITextField 的值:

let successAction: UIAlertAction = UIAlertAction(title: okLocalized,
            style: .Default) { action in
                success?(text: self.newQtyField.text!)
            }

关于ios - 快速代码返回警报类型字符串不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38517119/

相关文章:

ios - Firebase 自定义消息 iOS

ios - Firebase -Auth.auth().currentUser?.phoneNumber 返回 nil

ios - 使用 CMTimeMapping 寻找 AVComposition 会导致 AVPlayerLayer 卡住

swift - 在 Swift 中转换为泛型可选

ios - 重新绘制静态单元格并赋予它们新外观 IOS

iphone - 编译 nib2objc (Xcode)

ios - 无法使用 jsonExport 和 Codeables 解码 json

swift - if 语句导致无法解析的标识符

ios - 用于共享 vCard 的 UIActivity

ios - 创建自定义 SCNGeometry 多边形