我在UIAlertController下添加一个登录表单,以从网页获取一些xml数据。我需要输入用户名,密码和日期。为此,我使用.addTextFieldWithConfigurationHandler方法向UIAlertController的实例添加了3个文本字段。在其中一个中,我将输入视图更改为UIDatePicker。这笔交易是我需要添加一个带有UIBarButton项目的UIToolBar来制作一个“完成”按钮来完成输入。一切都做得很好,除了我无法访问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)
}
非常感谢,如果您有任何建议,请告诉我。
我是一名新手(而不是全职)程序员,如果我有任何方法可以对某人有用,请告诉我。
答案 0 :(得分:4)
UIAlertController
的一个属性是,当显示时,用户将无法与后台中的任何UI元素进行交互。因此,当警报控制器启动时,您希望用户能够使用的任何内容都需要成为该警报控制器的一部分。
这是一个很棒的tutorial by NSHipster,它超越了警报控制器。