Estoy tratando de mostrar un UIAlertControllercon un UITextView. Cuando agrego la línea:
//Add text field
alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
}
Me sale un Runtimeerror:
error fatal: inesperadamente encontrado nil al desenvolver un valor opcional
let alertController: UIAlertController = UIAlertController(title: "Find image", message: "Search for image", preferredStyle: .Alert)
//cancel button
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
//cancel code
}
alertController.addAction(cancelAction)
//Create an optional action
let nextAction: UIAlertAction = UIAlertAction(title: "Search", style: .Default) { action -> Void in
let text = (alertController.textFields?.first as! UITextField).text
println("You entered \(text)")
}
alertController.addAction(nextAction)
//Add text field
alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
textField.textColor = UIColor.greenColor()
}
//Present the AlertController
presentViewController(alertController, animated: true, completion: nil)
Esto se presenta dentro de mi a ViewControllertravés de un IBAction.
He descargado el código de aquí y funciona bien. Copié y pegué ese método en mi código y se rompe. La presencia del yo en la última línea no tiene ningún impacto.
ios
swift
uitextfield
uialertcontroller
Quintin Balsdon
fuente
fuente

UITextField, en lugar deUITextView?Respuestas:
Swift 5.1
Use este código, estoy ejecutando este código en mi aplicación con éxito.
Editado: versión Swift 3.0
fuente
Para agregar un campo de texto en Swift 3.0:
fuente
Entonces comencé a verificar para ver qué podría haber sido diferente en mi código al código de trabajo. Noté que mi ViewController se extiende
Lo que aparentemente significa que necesito configurar el delegado de cualquier niño UITextView:
fuente
Solución:
Rápido 4.2
Pruebe las siguientes líneas y vea si funciona:
Espero eso ayude.
fuente
alertController.addTextFieldcorrecto el orden de los dos ? Parece que "Segundo nombre" aparecería encima de "Nombre".¿Cómo agregar textField a AlertView? Hagámoslo breve y sencillo.
Esto funciona para Swift 3.0 y superior.
Primero, crea una instancia de un objeto de
UIAlertControllery luego le agrega un campo de texto accediendo aaddTextFieldmember ofUIAlertControllerclass.fuente
Para agregar alertController con un campo de texto (Swift 5)
fuente
Para Swift 4.0, puede usar esta muestra de código probado con éxito en mi proyecto:
fuente
En Swift 4.2 y Xcode 10.1
Alerta con dos campos de texto y lectura de datos de texto de campo de texto y alerta presente en la parte superior de todas las vistas .
Si desea presentar aleert en la parte superior de todas las vistas.
Aquí, desde el código anterior, elimine esta última línea
self.present(alert, animated:true, completion: nil)y agregue el código siguiente.Ahora llama así
fuente
Gran respuesta, una pequeña modificación para mostrar cómo se puede usar el campo de texto:
fuente
agregue TextField a UIAlertController y TextField text Display en UILabel en Swift
fuente
UIAlertControllercon laUITextfieldúltima versión de Apple Swift 5.1.3Cree una
lazyvariable deUIAlertControlleren suUIViewController, AgregarUITextFieldDelegate, Mostrar alerta deUIButtonacción :Codificación feliz !! :)
fuente
Swift5
La primera clase se ajusta a UITextFieldDelegate, luego crea una nueva propiedad textField
Gracias
fuente
Rápido 5
fuente