Xcode Swift:如何从UIButton调用UIPickerView?

时间:2015-02-17 05:43:04

标签: xcode swift

我需要在用户触摸UIButton后打开UIPickerView,并将UIPickerview上选择的文本值返回到UIButton标签。

我无法像UITextField那样更改UIButton'n输入视图,因此使属性可写成似乎是正确的方法。不幸的是,触摸按钮时没有任何反应。

import UIKit

class ABButton: UIButton {

var modInputView: UIView!

override var inputView: UIView { get {

    if modInputView != nil {
        return modInputView
    }
    else {
        return super.inputView!
    }

}}

override func canBecomeFirstResponder() -> Bool {
    return true

}
}


class LiczydloViewController: UIViewController {

@IBOutlet weak var buttonTempo10: ABButton!

override func viewDidLoad() {
    super.viewDidLoad()

    var tempoPicker = UIDatePicker()


    buttonTempo10.modInputView = tempoPicker

 }

2 个答案:

答案 0 :(得分:0)

为按钮添加touchUpInside操作,并调用button.becomeFirstResponder()

答案 1 :(得分:0)

尝试设置UIDatePicker的框架。我很幸运地试了一下。

class ZYInputButton: UIButton {
    var zyInputView: UIView?
    var zyInputAccessoryView: UIView?

    override var inputView: UIView? {
        get {
            return self.zyInputView
        }

        set {
            self.zyInputView = newValue
        }
    }

    override var inputAccessoryView: UIView? {
        get {
            return self.zyInputAccessoryView
        }

        set {
            self.zyInputAccessoryView = newValue
        }
    }

    override func canBecomeFirstResponder() -> Bool {
        return true
    }
}