Swift - 将一个pickerView添加到UIViewController中的文本字段中

时间:2016-08-27 11:08:27

标签: ios swift

我正在使用@IBOutlet在选项和设置视图和子视图方面苦苦挣扎。我目前正在尝试创建一个控制器来将用户添加到数据库中。我的一个字段与用户的性别有关,因此我希望将其链接到UIPickerView。 我没有使用Storyboard。

我从代码中删除了所有其他字段,使其更短更易读,但它也有其他UIPickerView字段(如生日)。

有人可以帮助我使用我的代码,以便我的文本字段在选中时会显示UIPickerView吗?

class AddUserController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {

var picker: UIPickerView = UIPickerView()

var genderData = ["Male", "Female"]

// Containers
let inputsContainerView: UIView = {
    let view = UIView()
    view.backgroundColor = UIColor.whiteColor()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.layer.masksToBounds = true
    return view
}()

// Subviews
@IBOutlet weak var genderTextField: UITextField! = {
    var gender = UITextField()
    gender.placeholder = "Gender"
    gender.translatesAutoresizingMaskIntoConstraints = false
    gender.leftViewMode = UITextFieldViewMode.Always
    gender.autocorrectionType = .No

    let lbl = UILabel(frame: CGRect(x: 0, y: 0, width: 120, height: 30))
    lbl.text = "GENDER"
    lbl.font = UIFont.boldSystemFontOfSize(12)
    gender.leftView = lbl
    return gender
}()

override func viewDidLoad() {
    super.viewDidLoad()

    picker = UIPickerView()

    picker.dataSource = self
    picker.delegate = self
    picker.hidden = true

    genderTextField?.delegate = self
    genderTextField?.inputView = picker
    genderTextField?.text = genderData[0]

    view.backgroundColor = UIColor(white: 0.95, alpha: 1)

    view.addSubview(inputsContainerView)

    setupInputsContainerView()
}

// Picker functions
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return genderData.count
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return genderData[row]
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
    genderTextField.text = genderData[row]
    self.view.endEditing(true)
    picker.hidden = true
}

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
    picker.hidden = false
    return true
}

// Setup all the views
func setupInputsContainerView() {
    inputsContainerView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor).active = true
    inputsContainerView.topAnchor.constraintEqualToAnchor(view.bottomAnchor, constant: 12).active = true
    inputsContainerView.widthAnchor.constraintEqualToAnchor(view.widthAnchor, constant: -24).active = true
    inputsContainerView.heightAnchor.constraintEqualToConstant(40).active = true

    inputsContainerView.addSubview(genderTextField)

    genderTextField.leftAnchor.constraintEqualToAnchor(inputsContainerView.leftAnchor, constant: 12).active = true
    genderTextField.topAnchor.constraintEqualToAnchor(inputsContainerView.topAnchor).active = true
    genderTextField.widthAnchor.constraintEqualToAnchor(inputsContainerView.widthAnchor).active = true
    genderTextField.heightAnchor.constraintEqualToAnchor(inputsContainerView.heightAnchor).active = true
}
}

1 个答案:

答案 0 :(得分:0)

每次调用时都会创建genderTextField。

genderTextField?.delegate = self
genderTextField?.inputView = picker
genderTextField?.text = genderData[0]

所有这些都是不同的对象

inputsContainerView.addSubview(genderTextField)

您在此处添加的内容也有所不同。因此,您添加到视图的textField和您添加的委托/数据源都是不同的。一个快速的解决方案是移动

genderTextField?.delegate = self
genderTextField?.inputView = picker
genderTextField?.text = genderData[0]

此行inputsContainerView.addSubview(genderTextField)

之后的这些代码