有没有办法将闭包传递给 UIAlertAction,它将 return 中的字符串 Swift?
Is there a way to pass a closure to UIAlertAction that will return a String in Swift?
我正在尝试更多地了解 Swift 闭包概念,但我遇到了这个问题,我有以下 UIAlertController 方法:
public static func showSerialNumberAlertController(handler: @escaping (UIAlertAction) -> String?) {
let alertController = UIAlertController(title: "Serial Number", message: "Please enter your serial number", preferredStyle: .alert)
// ADD ACTIONS HANDLER
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
cancelAction.isEnabled = true
alertController.addAction(cancelAction)
let continueAction = UIAlertAction(title: "Continue", style: .default) { (_) in
let serialNumberField = alertController.textFields![0] as UITextField
let text = serialNumberField.text
print("Serial number entered: \(text!)")
}
continueAction.isEnabled = false
alertController.addAction(continueAction)
// ADD TEXT FIELDS
alertController.addTextField { (textField) in
textField.placeholder = "Serial number"
// enable continue button when serial not empty
NotificationCenter.default.addObserver(forName: NSNotification.Name.UITextFieldTextDidChange, object: textField, queue: OperationQueue.main) { (notification) in
continueAction.isEnabled = textField.text != "" && textField.text?.count as! Int > 3
}
}
alertController.view.tintColor = Colors.mainColor
getRootViewController()?.present(alertController, animated: true)
}
现在我要做的是用我从外部收到的处理程序替换 continueAction
UIAlertAction
的处理程序,该处理程序将从序列号 UITextField
中提取文本] 当用户按下继续按钮并将其作为 returned 值传递时。
所以我在这个范围之外的另一个文件中有这个方法,我从中调用这个静态方法:
public func getSerialFromAlert(alertController: UIAlertController) -> String?
{
let serialNumberField = alertController.textFields![0] as UITextField
let text = serialNumberField.text
print("Serial number entered: \(text!)")
return text
}
我想将此方法作为处理程序传递,但是当我这样做时出现此错误:
Cannot convert value of type '(UIAlertAction) -> String?' to expected argument type '((UIAlertAction) -> Void)?'
所以问题是: 是否有可能实现这一点并传递一个将 return 值作为参数的处理程序?如果是这样,这样做的正确方法是什么?如果不是,对于必须访问警报视图以提取数据的 UIAlertController
操作委托,替代方案是什么?
如果我理解正确,你可以将你的方法签名更改为
showSerialNumberAlertController(completion: @escaping (String) -> Void)
并在此函数中将继续操作更改为:
let continueAction = UIAlertAction(title: "Continue", style: .default) { (_) in
let serialNumberField = alertController.textFields![0] as UITextField
let text = serialNumberField.text
completion(text ?? "")
}
最后你调用了这样的方法:
UIAlertController.showSerialNumberAlertController { serial in
print("Serial number entered: \(serial)")
}
我正在尝试更多地了解 Swift 闭包概念,但我遇到了这个问题,我有以下 UIAlertController 方法:
public static func showSerialNumberAlertController(handler: @escaping (UIAlertAction) -> String?) {
let alertController = UIAlertController(title: "Serial Number", message: "Please enter your serial number", preferredStyle: .alert)
// ADD ACTIONS HANDLER
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
cancelAction.isEnabled = true
alertController.addAction(cancelAction)
let continueAction = UIAlertAction(title: "Continue", style: .default) { (_) in
let serialNumberField = alertController.textFields![0] as UITextField
let text = serialNumberField.text
print("Serial number entered: \(text!)")
}
continueAction.isEnabled = false
alertController.addAction(continueAction)
// ADD TEXT FIELDS
alertController.addTextField { (textField) in
textField.placeholder = "Serial number"
// enable continue button when serial not empty
NotificationCenter.default.addObserver(forName: NSNotification.Name.UITextFieldTextDidChange, object: textField, queue: OperationQueue.main) { (notification) in
continueAction.isEnabled = textField.text != "" && textField.text?.count as! Int > 3
}
}
alertController.view.tintColor = Colors.mainColor
getRootViewController()?.present(alertController, animated: true)
}
现在我要做的是用我从外部收到的处理程序替换 continueAction
UIAlertAction
的处理程序,该处理程序将从序列号 UITextField
中提取文本] 当用户按下继续按钮并将其作为 returned 值传递时。
所以我在这个范围之外的另一个文件中有这个方法,我从中调用这个静态方法:
public func getSerialFromAlert(alertController: UIAlertController) -> String?
{
let serialNumberField = alertController.textFields![0] as UITextField
let text = serialNumberField.text
print("Serial number entered: \(text!)")
return text
}
我想将此方法作为处理程序传递,但是当我这样做时出现此错误:
Cannot convert value of type '(UIAlertAction) -> String?' to expected argument type '((UIAlertAction) -> Void)?'
所以问题是: 是否有可能实现这一点并传递一个将 return 值作为参数的处理程序?如果是这样,这样做的正确方法是什么?如果不是,对于必须访问警报视图以提取数据的 UIAlertController
操作委托,替代方案是什么?
如果我理解正确,你可以将你的方法签名更改为
showSerialNumberAlertController(completion: @escaping (String) -> Void)
并在此函数中将继续操作更改为:
let continueAction = UIAlertAction(title: "Continue", style: .default) { (_) in
let serialNumberField = alertController.textFields![0] as UITextField
let text = serialNumberField.text
completion(text ?? "")
}
最后你调用了这样的方法:
UIAlertController.showSerialNumberAlertController { serial in
print("Serial number entered: \(serial)")
}