SWIFT : UIAlertView with Textfield :(

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

Screenshot 2023-06-23 at 8.36.13 AM

SOLVED :slight_smile:


    public func showAlert(_ view:UIViewController,_ msgBoxID:String) {
        var inputTextField: UITextField?
        let alert = UIAlertController(title: self.title, message: self.message, preferredStyle: UIAlertController.Style.alert)
        //
        if asInput {
            alert.addTextField(configurationHandler: {(textField: ds$TEXTFIELD) in
                textField.placeholder        = "Enter Value"
                textField.keyboardType       = .decimalPad
                textField.autocorrectionType = .no
                inputTextField = textField
            })
        }
        //
        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 {
                    let s = inputTextField!.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)
    }

had to use another textfield (inputTextField)… dunno why