从UIAlertController访问输入

And*_*rew 48 swift ios8 uialertcontroller

我有一个UIAlertController,当他们在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)
Run Code Online (Sandbox Code Playgroud)

我知道OK按钮可能应该有一个处理程序,但是现在这段代码并没有真正做任何事情,但我想通过println显示文本字段的输出.这对我来说真的只是一个测试应用程序,可以学习Swift和一些新的API.

Ash*_*row 63

我写了一篇探索新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)
Run Code Online (Sandbox Code Playgroud)

这样就可以避免在视图控制器上出现不必要的属性.


Sco*_*ott 48

我知道已发布评论来回答这个问题,但答案应该使解决方案明确.

@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)
Run Code Online (Sandbox Code Playgroud)

这将显示带有文本字段和两个操作按钮的提示.它完成后会读取文本字段.


And*_*ndy 8

我将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)
Run Code Online (Sandbox Code Playgroud)


Gen*_*isa 7

在封闭内执行此操作:

let tf = alert.textFields[0] as UITextField
Run Code Online (Sandbox Code Playgroud)

这是一个要点