【问题标题】:Multiple UITextFields not displaying properly inside an UIAlertController多个 UITextFields 在 UIAlertController 中未正确显示
【发布时间】:2020-06-04 15:28:59
【问题描述】:

我需要在 UIAlertController (Xcode 11.5) 中显示 4 个 UITextField。照常进行,但中心 UITextFields 出现无边框 view image 并且看起来不太好(我正在添加屏幕截图)。我试图设置 UITextField 边框样式,框架,但我无法正确设置。

@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]

【问题讨论】:

  • 在我看来,四个文本字段似乎太多了...使用另一个 UIViewController 不是一个选项,也许以模态方式呈现?或者也许实现一个自定义的UIView,它将以一种模态的方式呈现,而不是使用默认的UIAlertController
  • 你是对的@mmika1000。但IMO它看起来并不那么糟糕。我需要用户填写 4 个字段才能创建“手术日”。用户将是医生。不,更糟糕的是……外科医生。所以我也尽量保持简单。

标签: swift uitextfield uialertcontroller xcode11


【解决方案1】:

试试这个解决方案。我建议不要捕获对UITextfields 的引用,也许这可能导致了这种行为。您可以通过UIAlertController.textFields 属性访问它们。通常所有的布局都是由UIAlertController 完成的,基本上没问题,我从来没有遇到过那里的错误。

还有。尽量避免使用! 强制解包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)
}



编辑: OP 实施的解决方案图片:

【讨论】:

  • 感谢您的建议和帮助@mmika1000。我认为您的代码更安全且更快捷,但它始终显示相同的布局行为。也许 UIAlertControllers 不应该显示 4 个 UITextFields,这是 Apple 告诉它的方式。也许我会尝试自定义控制器或您在上面评论的模态方法。非常感谢。
  • @FelipeAndai 哇,好吧,这真的很奇怪......也许你可以试试两个,然后三个,看看它是否能正常工作?无论如何,不​​客气!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多