My app contains this code
public func showAlert(_ view:UIViewController,_ msgBoxID:String) {
let alert = UIAlertController(title: self.title, message: self.message, preferredStyle: UIAlertController.Style.alert)
//
if asInput {
/* attempt #1
alert.addTextField() { newTextField in newTextField.placeholder = "Enter Value" }
alert.textFields![0].keyboardType = .decimalPad
*/
/* attempt #2
alert.addTextField(configurationHandler: {textField: ds$TEXTFIELD) in
textField.placeholder = "Enter Value"
textField.keyboardType = .decimalPad
textField.autocorrectionType = .no
})
*/
alert.addTextField { textField in
textField.placeholder = "Enter Value"
textField.autocorrectionType = .no
textField.keyboardType = .decimalPad
}
print("A) Alert Textfields added \(self.textFields?.count)")
}
//
if btns.count==0 { addBtn(.OK) }// require at least ONE Button
//
for i in(0...btns.count-1) {
alert.addAction(UIAlertAction(title: btns[i].caption, style: btns[i].style , handler:{(UIAlertAction) in
if self.btns[i].caption.uppercased() == "OK" && self.asInput==true {
print("B) Alert Textfield Found \(self.textFields?.count)")
let s = self.textFields?[0].text!
self.delegate?.msgBoxResult(caption:self.btns[i].caption, action:self.btns[i].action,msgBoxID: msgBoxID, result:s!)
} else {
self.delegate?.msgBoxAction(caption:self.btns[i].caption, action:self.btns[i].action,msgBoxID: msgBoxID)
}
}))
}
view.present(alert, animated: true, completion: nil)
}
if “asInput” is true it should add a UITextField to the alert box…
When the alert is displayed, the textfield is there, and seems to work just fine
HOWEVER.
the print("A) line says 0 fields were added
and worse
the print(“B)” line also says 0, and as such it crashes when attempt to get the entered value
as you can see I tried 3 different ways, all result the same