Felipe Andai
Felipe Andai

Reputation: 15

Multiple UITextFields not displaying properly inside an UIAlertController

I need to display 4 UITextFields in an UIAlertController (Xcode 11.5). Did it as usual but the center UITextFields appear w/o border view image and it doesn't look well (I am adding a screenshot). I tried to set the UITextField Borderstyle, frame, but I can't get it right.

@IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
    var dayOfWeekField = UITextField()
    var inicioTextField = UITextField()
    var terminoTextField = UITextField()
    var numeroDeCirugias = UITextField()

    let alert = UIAlertController(title: "Agregar Nuevo Bloque", message: "", preferredStyle: .alert)
    let action = UIAlertAction(title: "Agregar", style: .default) { (action) in

        let  nuevoBloque = Bloques(context: self.context)
        nuevoBloque.dia = dayOfWeekField.text!
        nuevoBloque.inicio = inicioTextField.text!
        nuevoBloque.termino = terminoTextField.text!
        nuevoBloque.cirugias = Int16(numeroDeCirugias.text!)!

        self.bloques.append(nuevoBloque)

        self.saveBloques()

    }

    alert.addAction(action)

    alert.addAction(UIAlertAction(title: "Cancelar", style: .destructive, handler: nil))

    alert.addTextField { (field) in
        dayOfWeekField = field
        dayOfWeekField.placeholder = "dia de la semana"
        dayOfWeekField.autocapitalizationType = .none

    }
    alert.addTextField { (field2) in
        inicioTextField = field2
        inicioTextField.placeholder = "Inicio Bloque (HH:MM)"
        inicioTextField.keyboardType = .numbersAndPunctuation
    }
    alert.addTextField { (field3) in
        terminoTextField = field3
        terminoTextField.placeholder = "Término Bloque (HH:MM)"
        terminoTextField.keyboardType = .numbersAndPunctuation
    }
    alert.addTextField { (field4) in
        numeroDeCirugias = field4
        numeroDeCirugias.placeholder = "Número Cirugías"
        numeroDeCirugias.keyboardType = .numberPad
    }
    present(alert, animated: true, completion: nil)
}[enter image description here][1]

Upvotes: 1

Views: 62

Answers (1)

emmics
emmics

Reputation: 1063

Try this solution. I suggest to not capture references to the UITextfields, maybe that might have caused the behavior. You can Access them by UIAlertController.textFields property. Usually all layouting is done by the UIAlertController basically fine, I never encountered bugs there.

Also. try to avoid force unwrapping with ! by using guard let

@IBAction func addButtonPressed(_ sender: UIBarButtonItem) {

    let alert = UIAlertController(title: "Agregar Nuevo Bloque", message: "", preferredStyle: .alert)
    let action = UIAlertAction(title: "Agregar", style: .default) { (_) in

        let nuevoBloque = Bloques(context: self.context)

        guard
            let dayOfWeek = alert.textFields?[0].text,
            let inicio = alert.textFields?[1].text,
            let temino = alert.textFields?[2].text,
            let numeroDeCirugiasString = alert.textFields?[3].text,
            let numeroDeCirugias = Int(numeroDeCirugiasString)
            else {
                // not all textfields were implemented or filled out properly
                return
        }
        nuevoBloque.dia = dayOfWeek
        nuevoBloque.inicio = inicio
        nuevoBloque.termino = termino
        nuevoBloque.cirugias = numeroDeCirugias

        self.bloques.append(nuevoBloque)
        self.saveBloques()

    }

    alert.addAction(action)
    alert.addAction(UIAlertAction(title: "Cancelar", style: .cancel, handler: nil))

    alert.addTextField { (field) in
        field.placeholder = "dia de la semana"
        field.autocapitalizationType = .none
    }
    alert.addTextField { (field2) in
        field2.placeholder = "Inicio Bloque (HH:MM)"
        field2.keyboardType = .numbersAndPunctuation
    }
    alert.addTextField { (field3) in
        field3.placeholder = "Término Bloque (HH:MM)"
        field3.keyboardType = .numbersAndPunctuation
    }
    alert.addTextField { (field4) in
        field4.placeholder = "Número Cirugías"
        field4.keyboardType = .numberPad
    }
    present(alert, animated: true, completion: nil)
}



EDIT: Image of the Solution implemented by OP:

screenshot with your suggestion

Upvotes: 1

Related Questions