我在 UIAlertController 下添加了一个登录表单,以从网页获取一些 xml 数据。我需要输入用户名、密码和日期。为此,我使用 .addTextFieldWithConfigurationHandler 方法将 3 个文本字段添加到 UIAlertController 的实例。在其中一个中,我将输入 View 更改为 UIDatePicker。交易是我需要添加一个 UIToolBar 和一个 UIBarButton 项目来制作一个“完成”按钮来完成输入。一切都做得很好,除了我无法访问 showSelectedDate() 函数(从未调用该函数)以关闭 inputView 并使用日期设置 UITextField.text 属性。按钮在那里,但无法使用。
这是我的代码。
loginDeCompraMercadoWebAlertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
//Declare my inputView, DatePicker and UIToolBar
let toolBar = UIToolbar(frame: CGRectMake(0, 0, textField.frame.size.width, textField.frame.size.height))
let datePicker = UIDatePicker()
let formatter = NSDateFormatter()
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "showSelectedDate")
let space = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
toolBar.setItems([doneButton, space], animated: true)
//Setup inputView and placeholders
textField.placeholder = "Fecha de Compra"
textField.inputView = datePicker
textField.inputAccessoryView = toolBar
textField.text = formatter.stringFromDate(datePicker.date)
}
非常感谢,如果您有任何建议,请告诉我。
我是一名新手(而非全职)程序员,如果有什么方法可以对某人有用,也请告诉我。
最佳答案
UIAlertController
的一个属性是当它显示时,用户将无法与后台的任何 UI 元素进行交互。因此,当警报 Controller 启动时,您希望用户能够使用的任何东西都需要成为该警报 Controller 的一部分。
这是一个很棒的tutorial by NSHipster遍历警报 Controller 。
关于ios - Swift:如何在 UIAlertController 中添加自定义输入 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33835675/