简体   繁体   English

如何在 Swift 中使用 UIButton 激活 inputView?

[英]How do I activate an inputView using a UIButton in Swift?

I am attempting to have a UIDatePicker come up as a keyboard when the user hits a UIButton.当用户点击 UIButton 时,我试图让 UIDatePicker 作为键盘出现。 I was able to get it to work with a textfield, but I don't like how the cursor is visible and the user could enter in any text if they had an external keyboard.我能够让它与文本字段一起工作,但我不喜欢光标的可见性,如果用户有外部键盘,他们可以输入任何文本。 Here is my code:这是我的代码:

@IBAction func dateFieldStart(sender: UITextField) {
        var datePickerStartView  : UIDatePicker = UIDatePicker()
        datePickerStartView.datePickerMode = UIDatePickerMode.Time
        sender.inputView = datePickerStartView    // error when sender is UIButton
}

I tried changing the sender to UIButton but it gave this error on the line that is marked above:我尝试将发件人更改为 UIButton 但它在上面标记的行上给出了这个错误:

Cannot assign to 'inputView' in 'sender'

I have tried researching it and no one else seems to have had a problem with it.我试过研究它,似乎没有其他人遇到过它的问题。 Anyone know how to trigger a UIDatePicker inputView using a UIButton or anything that might work better that the user cannot type into?任何人都知道如何使用 UIButton 或任何用户无法输入的更好用的东西来触发 UIDatePicker inputView? Thanks!谢谢!

This is years after the original question, but for anyone who may be looking for solution to this you can subclass UIButton and provide a getter and setter for the inputView property.这是原始问题的数年后,但对于可能正在寻找解决方案的任何人,您都可以继承 UIButton 并为 inputView 属性提供 getter 和 setter。 Be sure to call becomeFirstResponder in the setter and override canBecomeFirstResponder.确保在 setter 中调用 becomeFirstResponder 并覆盖 canBecomeFirstResponder。 For example:例如:

class MyButton: UIButton {

    var myView: UIView? = UIView()
    var toolBarView: UIView? = UIView()
    
    override var inputView: UIView? {
        get {
            myView
        }
        
        set {
            myView = newValue
            becomeFirstResponder()
        }
    }

    override var inputAccessoryView: UIView? {
        get {
            toolBarView
        }
        set {
            toolBarView = newValue
        }
    }
    
    override var canBecomeFirstResponder: Bool {
       true
    }

}
let tempInput = UITextField( frame:CGRect.zero )
tempInput.inputView = self.myPickerView       // Your picker
self.view.addSubview( tempInput )
tempInput.becomeFirstResponder()

It's a good idea to keep a reference to tempInput so you can clean-up on close保留对tempInput的引用是个好主意,这样您就可以在关闭时进行清理

I wanted to do the same thing, I ended up just overlaying a UITextField over the button and using the inputView of that instead.我想做同样的事情,我最终只是在按钮上覆盖了一个 UITextField 并使用它的 inputView 代替。

Tip: set tintColor of the UITextField to UIColor.clearColor() to hide the cursor.提示:将 UITextField 的tintColor设置为UIColor.clearColor()以隐藏光标。

You can create a view for the picker off screen view and move it on screen when you need it.您可以为选择器屏幕外视图创建一个视图,并在需要时将其移动到屏幕上。 Here's another post on this.这是关于此的另一篇文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM