如何在不同 class 中使用 UIAlert 获取文本字段值? Swift
How to get textfield value with UIAlert in different class? Swift
我制作了一个包含不同类型通用 UIAlert
代码的文件,以便我可以重复使用它们。我的问题是如何从另一个 class 获取 textfield value
?到目前为止,我找到的所有答案都编码在同一个 class 中,我不希望这样。谢谢。
UIAlert 文件
func alertVerify(title: String, message: String, sender: UIViewController, verifyActionCompletionHandler: ((UIAlertAction) -> Void)? = nil, resendActionCompletionHandler: ((UIAlertAction) -> Void)? = nil) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let verifyAction = UIAlertAction(title: "Verify", style: .default, handler: verifyActionCompletionHandler)
alert.addAction(verifyAction)
let resendAction = UIAlertAction(title: "Resend", style: .default, handler: resendActionCompletionHandler)
alert.addAction(resendAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alert.addAction(cancelAction)
alert.addTextField(configurationHandler: { textField in
textField.placeholder = "Verification code"
})
DispatchQueue.main.async(execute: {
sender.present(alert, animated: true)
})
}
ViewController
func verifyEmail() {
guard let email = inputTextField.text else {
return
}
alertVerify(title: "Email Verification Code", message: "Enter the verification code we send in your updated email adress.", sender: self, verifyActionCompletionHandler: { UIAction in
if let inputCode = alert.textFields?.first?.text { //error: unresolved identifier 'alert'
print("Verification code: \(inputCode)")
//Do something
}
}, resendActionCompletionHandler: { UIAction in
self.updateData(updateItem: "email", updateData: ["email": email], endpoint: "info")
})
}
您也可以向您的方法添加另一个参数以传递警报控制器:
func alertVerify(title: String, message: String, sender: UIViewController, alert: UIAlertController, verify: ((UIAlertAction) -> Void)? = nil, resend: ((UIAlertAction) -> Void)? = nil) {
alert.title = title
alert.message = message
alert.addTextField {
[=10=].placeholder = "Verification code"
}
alert.addAction(.init(title: "Verify", style: .default, handler: verify))
alert.addAction(.init(title: "Resend", style: .default, handler: resend))
alert.addAction(.init(title: "Cancel", style: .cancel))
DispatchQueue.main.async {
sender.present(alert, animated: true)
}
}
然后当您调用您的方法时,您将警报控制器的实例传递给它:
func verifyEmail() {
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
alertVerify(title: "Email Verification Code",
message: "Enter the verification code we send in your updated email adress.",
sender: self,
alert: alert,
verify: { _ in
let inputCode = alert.textFields![0].text!
print("Verification code: \(inputCode)")
}, resend: nil)
}
另一种选择是子类化 UIAlertController 并向其添加您的自定义方法:
AlertController.swift
import UIKit
class AlertController: UIAlertController {
var controller: UIViewController!
convenience init(title: String?, message: String?, sender: UIViewController) {
self.init(title: title, message: message, preferredStyle: .alert)
controller = sender
}
func verify(_ action: ((UIAlertAction) -> Void)? = nil, resend: ((UIAlertAction) -> Void)? = nil) {
if actions.isEmpty {
addTextField {
[=12=].placeholder = "Verification code"
}
addAction(.init(title: "Verify", style: .default, handler: action))
addAction(.init(title: "Resend", style: .default, handler: resend))
addAction(.init(title: "Cancel", style: .cancel))
}
DispatchQueue.main.async {
self.controller.present(self, animated: true)
}
}
}
然后实例化您的自定义警报控制器并从中调用您的方法:
func verifyEmail() {
let alert = AlertController(title: "Email Verification Code", message: "Enter the verification code we send in your updated email adress.", sender: self)
alert.verify({ _ in
let inputCode = alert.textFields![0].text!
print("Verification code: \(inputCode)")
}, resend: nil)
}
我制作了一个包含不同类型通用 UIAlert
代码的文件,以便我可以重复使用它们。我的问题是如何从另一个 class 获取 textfield value
?到目前为止,我找到的所有答案都编码在同一个 class 中,我不希望这样。谢谢。
UIAlert 文件
func alertVerify(title: String, message: String, sender: UIViewController, verifyActionCompletionHandler: ((UIAlertAction) -> Void)? = nil, resendActionCompletionHandler: ((UIAlertAction) -> Void)? = nil) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let verifyAction = UIAlertAction(title: "Verify", style: .default, handler: verifyActionCompletionHandler)
alert.addAction(verifyAction)
let resendAction = UIAlertAction(title: "Resend", style: .default, handler: resendActionCompletionHandler)
alert.addAction(resendAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alert.addAction(cancelAction)
alert.addTextField(configurationHandler: { textField in
textField.placeholder = "Verification code"
})
DispatchQueue.main.async(execute: {
sender.present(alert, animated: true)
})
}
ViewController
func verifyEmail() {
guard let email = inputTextField.text else {
return
}
alertVerify(title: "Email Verification Code", message: "Enter the verification code we send in your updated email adress.", sender: self, verifyActionCompletionHandler: { UIAction in
if let inputCode = alert.textFields?.first?.text { //error: unresolved identifier 'alert'
print("Verification code: \(inputCode)")
//Do something
}
}, resendActionCompletionHandler: { UIAction in
self.updateData(updateItem: "email", updateData: ["email": email], endpoint: "info")
})
}
您也可以向您的方法添加另一个参数以传递警报控制器:
func alertVerify(title: String, message: String, sender: UIViewController, alert: UIAlertController, verify: ((UIAlertAction) -> Void)? = nil, resend: ((UIAlertAction) -> Void)? = nil) {
alert.title = title
alert.message = message
alert.addTextField {
[=10=].placeholder = "Verification code"
}
alert.addAction(.init(title: "Verify", style: .default, handler: verify))
alert.addAction(.init(title: "Resend", style: .default, handler: resend))
alert.addAction(.init(title: "Cancel", style: .cancel))
DispatchQueue.main.async {
sender.present(alert, animated: true)
}
}
然后当您调用您的方法时,您将警报控制器的实例传递给它:
func verifyEmail() {
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
alertVerify(title: "Email Verification Code",
message: "Enter the verification code we send in your updated email adress.",
sender: self,
alert: alert,
verify: { _ in
let inputCode = alert.textFields![0].text!
print("Verification code: \(inputCode)")
}, resend: nil)
}
另一种选择是子类化 UIAlertController 并向其添加您的自定义方法:
AlertController.swift
import UIKit
class AlertController: UIAlertController {
var controller: UIViewController!
convenience init(title: String?, message: String?, sender: UIViewController) {
self.init(title: title, message: message, preferredStyle: .alert)
controller = sender
}
func verify(_ action: ((UIAlertAction) -> Void)? = nil, resend: ((UIAlertAction) -> Void)? = nil) {
if actions.isEmpty {
addTextField {
[=12=].placeholder = "Verification code"
}
addAction(.init(title: "Verify", style: .default, handler: action))
addAction(.init(title: "Resend", style: .default, handler: resend))
addAction(.init(title: "Cancel", style: .cancel))
}
DispatchQueue.main.async {
self.controller.present(self, animated: true)
}
}
}
然后实例化您的自定义警报控制器并从中调用您的方法:
func verifyEmail() {
let alert = AlertController(title: "Email Verification Code", message: "Enter the verification code we send in your updated email adress.", sender: self)
alert.verify({ _ in
let inputCode = alert.textFields![0].text!
print("Verification code: \(inputCode)")
}, resend: nil)
}