ios - Swift:如何在 UIAlertController 中添加自定义输入 View ?

标签 ios swift cocoa-touch swift2 uialertcontroller

我在 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/

相关文章:

iphone - 如何优化全文搜索的 Core Data 查询

ios - 如何将数据从一个 View Controller 传递到另一个 SWIFT

ios - 如何检测 InappPurchase 中正在加载哪个国家/地区的价格?

ios - 如何获取 UITableView 中的所有单元格

swift - 使粒子系统仅受 SceneKit 中某些类别的物理场的影响?

objective-c - UISplitViewController 中缺少 UIBarButtonItem

objective-c - 返回 NavigationController Root View 时会调用哪些方法?

ios - Swift:第一次单击 UITextField 时立即消失

swift - 使用观察者模式设置委托(delegate)时遇到问题

swift - 类型 'Int' 不符合协议(protocol) 'BooleanType'?