Вернуть текст из UIAlertController в расширение - PullRequest
1 голос
/ 12 апреля 2019

Как в 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")
    }
}

но я думаю, что это не правильный путь.

1 Ответ

4 голосов
/ 12 апреля 2019

Использовать обработчик завершения.

extension UIViewController {
    func presentTextFieldAlert(title: String, message: String, textFieldPlaceholder: String, completion: @escaping (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
            completion(urlTextField.text)
        }

        let cancelAction = UIAlertAction(title: "Cancel", style: .default)
        alertController.addTextField { (textField: UITextField!) -> Void in
            textField.placeholder = textFieldPlaceholder
            completion(nil)
        }
        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") { (result) in
            if let result = result {
                // User entered some text and tapped OK.
            }
        }
    }
}
...