Вход для доступа от UIAlertController
У меня есть UIAlertController, который предлагается пользователю, когда они выбирают "Enter Password" на экране TouchID. Как восстановить пользовательский ввод после представления этого на экране?
let passwordPrompt = UIAlertController(title: "Enter Password", message: "You have selected to enter your passwod.", preferredStyle: UIAlertControllerStyle.Alert)
passwordPrompt.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil))
passwordPrompt.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
passwordPrompt.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
textField.placeholder = "Password"
textField.secureTextEntry = true
})
presentViewController(passwordPrompt, animated: true, completion: nil)
Я знаю, что кнопка OK должна, вероятно, иметь обработчик, но сейчас этот код на самом деле ничего не делает, но я хочу отобразить вывод текстового поля через println. Это действительно просто тестовое приложение для меня, чтобы изучить Swift и некоторые новые материалы API.
Ответы
Ответ 1
Я знаю, что комментарии были отправлены для ответа на вопрос, но ответ должен сделать явное решение.
@IBOutlet var newWordField: UITextField
func wordEntered(alert: UIAlertAction!){
// store the new word
self.textView2.text = deletedString + " " + self.newWordField.text
}
func addTextField(textField: UITextField!){
// add the text field and make the result global
textField.placeholder = "Definition"
self.newWordField = textField
}
// display an alert
let newWordPrompt = UIAlertController(title: "Enter definition", message: "Trainging the machine!", preferredStyle: UIAlertControllerStyle.Alert)
newWordPrompt.addTextFieldWithConfigurationHandler(addTextField)
newWordPrompt.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil))
newWordPrompt.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: wordEntered))
presentViewController(newWordPrompt, animated: true, completion: nil)
Отображает приглашение с текстовым полем и двумя кнопками действий. Он читает текстовое поле, когда это делается.
Ответ 2
Я написал сообщение , изучающее новый API. Вы можете просто захватить локальную переменную в закрытии, и вам хорошо идти.
var inputTextField: UITextField?
let passwordPrompt = UIAlertController(title: "Enter Password", message: "You have selected to enter your password.", preferredStyle: UIAlertControllerStyle.Alert)
passwordPrompt.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil))
passwordPrompt.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
// Now do whatever you want with inputTextField (remember to unwrap the optional)
}))
passwordPrompt.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
textField.placeholder = "Password"
textField.secureTextEntry = true
inputTextField = textField
})
presentViewController(passwordPrompt, animated: true, completion: nil)
Таким образом, вы избегаете ненужного свойства на вашем контроллере просмотра.
Ответ 3
Я портировал ответ Ash Furrow на Swift 3.
var inputTextField: UITextField?
let passwordPrompt = UIAlertController(title: "Enter Password", message: "You have selected to enter your password.", preferredStyle: UIAlertControllerStyle.alert)
passwordPrompt.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default, handler: nil))
passwordPrompt.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { (action) -> Void in
// Now do whatever you want with inputTextField (remember to unwrap the optional)
}))
passwordPrompt.addTextField(configurationHandler: {(textField: UITextField!) in
textField.placeholder = "Password"
textField.isSecureTextEntry = true
inputTextField = textField
})
present(passwordPrompt, animated: true, completion: nil)
Ответ 4
Сделайте это в своем закрытии:
let tf = alert.textFields[0] as UITextField
Вот суть