Как в Swift я могу вернуть строку, которую я пишу в текстовое поле UIAlertController, добавленную, если этот UIAlertController находится внутри расширения
Обычно, если вы поместите реализацию UIAlertController локально в своем классе, вы можете легко передать текст, но когда предупреждение входит в расширение, я не уверен, какой способ вернуть текст.
Например, предположим, что у вас есть это расширение:
extension UIViewController {
func presentTextFieldAlert(title: String, message: String, textFieldPlaceholder: String ) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let saveAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.default) { _ -> Void in
let urlTextField = alertController.textFields![0] as UITextField
if urlTextField.text != nil { }
}
let cancelAction = UIAlertAction(title: "Cancel", style: .default)
alertController.addTextField { (textField: UITextField!) -> Void in
textField.placeholder = textFieldPlaceholder
}
alertController.addAction(saveAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}
}
и в ваш класс:
class Client: UIViewController {
func showAlert() {
self.presentTextFieldAlert(title: "Hello", message: "Write sth", textFieldPlaceholder: "Write here")
}
}
как я могу передать текст из предупреждения в viewcontroller?
Я пробовал что-то вроде:
class Client: UIViewController {
func showAlert() -> String {
return self.presentTextFieldAlert(title: "Hello", message: "Write sth", textFieldPlaceholder: "Write here")
}
}
но я думаю, что это не правильный путь.