如何将 UIAlertController 输出到单独的文件并从那里输出数据?
How can I output UIAlertController to a separate file and output data from there?
我正在尝试将警报输出到一个单独的函数,因为会有很多类似的函数。
这是我的提醒:
extension UIViewController {
func alertEditSum(nameCell: String, completion: (() -> Void)) {
let alertController = UIAlertController(title: "Hello", message: "", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Save", style: .default , handler: { _ in
let nameFolderField = alertController.textFields![0] as UITextField
if nameFolderField.isTextFieldCheck(text: nameFolderField.text!) == true {
// -----here----
}
}))
alertController.addAction(UIAlertAction(title: "Cancel"
, style: .cancel, handler: nil))
alertController.addTextField(configurationHandler: { (nameField: UITextField!) -> Void in
nameField.clearButtonMode = .always
nameField.keyboardType = .decimalPad
})
self.present(alertController, animated: true)
}
}
我的代码在另一个 VC:
self.sortedDate[indexPath.section-1].personPayment = Double(nameFolderField.text!)!
do {
try! self.context.save()
collectionView.reloadData()
}
问题是我需要考虑用户在 UITextField 中输入的内容。文本! (nameFolderField.text!)。我也无法将完成添加到警报代码,写入错误。
完成应该添加到它说的行:/ / - - - - - 这里----
请告诉我如何解决这个问题?
你需要 @escaping 完成将值从闭包内部发送给调用者,主要更改 completion:@escaping (String?) -> Void) 和 完成(nameFolderField.text)
extension UIViewController {
func alertEditSum(nameCell: String,completion:@escaping (String?) -> Void) {
let alertController = UIAlertController(title: "Hello", message: "", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Save", style: .default , handler: { _ in
let nameFolderField = alertController.textFields!.first!
if nameFolderField.isTextFieldCheck(text: nameFolderField.text!) {
completion(nameFolderField.text)
}
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alertController.addTextField(configurationHandler: { (nameField: UITextField!) -> Void in
nameField.clearButtonMode = .always
nameField.keyboardType = .decimalPad
})
self.present(alertController, animated: true)
}
}
通话
alertEditSum(nameCell:<#SomeValue#>) { result in
print(result)
}
这应该可以解决问题:
func alertEditSum(nameCell: String, completion: @escaping ((String?) -> Void)) {
let alertController = UIAlertController(title: "Hello", message: "", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Save", style: .default, handler: { _ in
let nameFolderField = alertController.textFields![0] as UITextField
if nameFolderField.isTextFieldCheck(text: nameFolderField.text!) == true {
completion(nameFolderField.text)
}
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { _ in
completion(nil)
}))
alertController.addTextField(configurationHandler: { nameField in
nameField.clearButtonMode = .always
nameField.keyboardType = .decimalPad
})
self.present(alertController, animated: true)
}
调用它:
theViewController.alertEditSum(nameCell: "text") { text in
if let text = text {
//Do stuff
} else { //Text is invalid or user has cancel
}
}
现在,我猜 isTextFieldCheck(text:)
是 UITextField
上的一个方法,因为它正在检查自己的文本,为什么将它作为参数提供?
为什么不只是 func isTextValid()
?
我也会避免强制解包:!
.
更进一步,将在完成中使用 Result<String, Error>
:
completion((Result<String, Error>) -> Void))
在需要时获得更多信息(用户已取消,文本因任何原因无效等)
我正在尝试将警报输出到一个单独的函数,因为会有很多类似的函数。
这是我的提醒:
extension UIViewController {
func alertEditSum(nameCell: String, completion: (() -> Void)) {
let alertController = UIAlertController(title: "Hello", message: "", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Save", style: .default , handler: { _ in
let nameFolderField = alertController.textFields![0] as UITextField
if nameFolderField.isTextFieldCheck(text: nameFolderField.text!) == true {
// -----here----
}
}))
alertController.addAction(UIAlertAction(title: "Cancel"
, style: .cancel, handler: nil))
alertController.addTextField(configurationHandler: { (nameField: UITextField!) -> Void in
nameField.clearButtonMode = .always
nameField.keyboardType = .decimalPad
})
self.present(alertController, animated: true)
}
}
我的代码在另一个 VC:
self.sortedDate[indexPath.section-1].personPayment = Double(nameFolderField.text!)!
do {
try! self.context.save()
collectionView.reloadData()
}
问题是我需要考虑用户在 UITextField 中输入的内容。文本! (nameFolderField.text!)。我也无法将完成添加到警报代码,写入错误。
完成应该添加到它说的行:/ / - - - - - 这里----
请告诉我如何解决这个问题?
你需要 @escaping 完成将值从闭包内部发送给调用者,主要更改 completion:@escaping (String?) -> Void) 和 完成(nameFolderField.text)
extension UIViewController {
func alertEditSum(nameCell: String,completion:@escaping (String?) -> Void) {
let alertController = UIAlertController(title: "Hello", message: "", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Save", style: .default , handler: { _ in
let nameFolderField = alertController.textFields!.first!
if nameFolderField.isTextFieldCheck(text: nameFolderField.text!) {
completion(nameFolderField.text)
}
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alertController.addTextField(configurationHandler: { (nameField: UITextField!) -> Void in
nameField.clearButtonMode = .always
nameField.keyboardType = .decimalPad
})
self.present(alertController, animated: true)
}
}
通话
alertEditSum(nameCell:<#SomeValue#>) { result in
print(result)
}
这应该可以解决问题:
func alertEditSum(nameCell: String, completion: @escaping ((String?) -> Void)) {
let alertController = UIAlertController(title: "Hello", message: "", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Save", style: .default, handler: { _ in
let nameFolderField = alertController.textFields![0] as UITextField
if nameFolderField.isTextFieldCheck(text: nameFolderField.text!) == true {
completion(nameFolderField.text)
}
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { _ in
completion(nil)
}))
alertController.addTextField(configurationHandler: { nameField in
nameField.clearButtonMode = .always
nameField.keyboardType = .decimalPad
})
self.present(alertController, animated: true)
}
调用它:
theViewController.alertEditSum(nameCell: "text") { text in
if let text = text {
//Do stuff
} else { //Text is invalid or user has cancel
}
}
现在,我猜 isTextFieldCheck(text:)
是 UITextField
上的一个方法,因为它正在检查自己的文本,为什么将它作为参数提供?
为什么不只是 func isTextValid()
?
我也会避免强制解包:!
.
更进一步,将在完成中使用 Result<String, Error>
:
completion((Result<String, Error>) -> Void))
在需要时获得更多信息(用户已取消,文本因任何原因无效等)