UIPickerView弹出窗口

时间:2016-03-24 03:55:53

标签: ios swift uitextfield uipickerview

我生成的代码有什么问题?我试图在点击文本字段时弹出UIPickerView,然后在做出选择后离开。文本字段应该显示之后选择的内容。

import UIKit

class CreateAJob_View_ControllerViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
    @IBOutlet var pickerTextField: UITextField!
    @IBOutlet var picker: UIPickerView!

    var pickerData:[String] = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
        picker.hidden = true;
        pickerTextField.text = "A4";

        self.pickerTextField.delegate = self
        self.picker.delegate = self
        self.picker.dataSource = self

         pickerData = ["A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","A10"]
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

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

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

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

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        pickerTextField.text = pickerData[row];
        picker.hidden = true;
    }

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

1 个答案:

答案 0 :(得分:0)

您需要以这种方式更改这两种方法:

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    self.pickerTextField.hidden = false;
    self.picker.hidden = true;
    self.pickerTextField.text = pickerData[row];
}

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