简体   繁体   English

如何使用警报在iOS中使用Swift提示文本?

[英]How do you use an alert to prompt for text in iOS with Swift?

func addPitcher(sender: UIBarButtonItem) {
    var alert = UIAlertController(title: "New Pitcher", message: "Enter Name", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Finish", style: UIAlertActionStyle.Default, handler: nil))
    alert.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
        textField.placeholder = "Name"
        textField.secureTextEntry = false
    })
    self.presentViewController(alert, animated: true, completion: nil)
    let newName : String = alert.textFields[0] as String
    println(newName)
}

This is the function in which we try and create an alert to prompt for a name. 这是我们尝试创建警报以提示姓名的功能。 We get "EXC_BAD_INSTRUCTION" error at the alert.addTextFieldWithConfigurationHandler({(textField: UITextField!) in line. 我们在alert.addTextFieldWithConfigurationHandler({(textField: UITextField!) in收到“EXC_BAD_INSTRUCTION”错误。

How do we fix this error, or more importantly, how do we retrieve the text from the field? 我们如何修复此错误,或者更重要的是,我们如何从字段中检索文本? Thank you for all help. 谢谢你的帮助。

You're getting a textfield as a string. 您将文本字段作为字符串。 Blammo. Blammo。

Also, get the value in the handler. 另外,获取处理程序中的值。

alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler:{ (alertAction:UIAlertAction!) in
            let textf = alert.textFields[0] as UITextField
            println(textf.text)
            }))

ps sometimes the error shown happens in a location different from the one given in the error message. ps有时显示的错误发生在与错误消息中给出的位置不同的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM