Swift: 如何在 UIAlertController 中添加自定义输入视图?
Swift: how to add a custom input view in an UIAlertController?
我正在 UIAlertController 下添加一个登录表单,以从网页获取一些 xml 数据。我需要输入用户名、密码和日期。为此,我使用 .addTextFieldWithConfigurationHandler 方法将 3 个文本字段添加到 UIAlertController 的实例。在其中一个中,我将输入视图更改为 UIDatePicker。问题是我需要添加一个带有 UIBarButton 项的 UIToolBar 来制作一个 "Done" 按钮来完成输入。一切都做得很好,除了我无法访问 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 元素进行交互。因此,当警报控制器启动时,您希望用户能够使用的任何东西都需要成为该警报控制器的一部分。
这是一个很好的 tutorial by NSHipster 警报控制器。
我正在 UIAlertController 下添加一个登录表单,以从网页获取一些 xml 数据。我需要输入用户名、密码和日期。为此,我使用 .addTextFieldWithConfigurationHandler 方法将 3 个文本字段添加到 UIAlertController 的实例。在其中一个中,我将输入视图更改为 UIDatePicker。问题是我需要添加一个带有 UIBarButton 项的 UIToolBar 来制作一个 "Done" 按钮来完成输入。一切都做得很好,除了我无法访问 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 元素进行交互。因此,当警报控制器启动时,您希望用户能够使用的任何东西都需要成为该警报控制器的一部分。
这是一个很好的 tutorial by NSHipster 警报控制器。