UITEXTFIELD InputView Uipicker无法在swift中运行

时间:2016-01-28 09:35:30

标签: ios swift uipickerview uipickerviewdatasource

你好我试图在用户点击文本字段时以编程方式弹出UIPickerView视图。我试过这个,但它没有做任何事情。它不起作用。单击文本域时没有发生任何事情

class UserProfileTableViewController: UITableViewController,UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource {
 var itemPicker: UIPickerView! = UIPickerView()

    @IBOutlet weak var genderTxtField: UITextField!


    var gender = ["MALE","FEMALE"]
    override func viewDidLoad() {
        super.viewDidLoad()


        genderTxtField.delegate = self

      itemPicker!.delegate = self
        itemPicker!.dataSource = self
        itemPicker!.backgroundColor = UIColor.blackColor()

         self.genderTxtField.inputView = itemPicker





    }




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

    // returns the # of rows in each component..
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
        return gender.count
    }

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

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {
        genderTxtField.text = gender[row]
        //.hidden = true;
    }

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

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


}

4 个答案:

答案 0 :(得分:1)

您当前代码中的问题在于此方法:

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
    itemPicker.hidden = false 
    return false // <--- this is not letting the textField become editable
}

我建议完全删除此方法。

答案 1 :(得分:1)

好的,所以我挖出了类似于我写的东西,它使用了一个日期选择器,但一般代码是相同的。

var datePicker: UIDatePicker!
@IBOutlet var dateFrom: UITextField!

override func viewDidLoad() {
   super.viewDidLoad()
   self.setUpDatePicker()
}

func setUpDatePicker() {
        datePicker.datePickerMode = UIDatePickerMode.Date
        let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 0, height: 44))
        let doneBtn = UIBarButtonItem(title: "Select", style: UIBarButtonItemStyle.Done, target: self, action: "selectDate")
        let space = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)

        toolBar.setItems([space, doneBtn], animated: false)

        YOURTEXTFIELD.inputAccessoryView = toolBar
        YOURTEXTFIELD.inputView = datePicker             
    }

然后添加一个功能来处理完成按钮

func selectDate() {
        let formatter = NSDateFormatter()
        formatter.dateFormat = "dd-MM-yyyy";

        YOURTEXTFIELD.text = formatter.stringFromDate(datePicker.date)
        YOURTEXTFIELD.resignFirstResponder()
    }

只需将您的版本genderTxtField替换为YOURTEXTFIELD,然后将日期选择器更改为您的选择器。

不要忘记检查是否所有内容都已链接

答案 2 :(得分:0)

itemPicker!.backgroundColor = UIColor.blackColor() 

更改为

 itemPicker!.backgroundColor = UIColor.whiteColor()

然后删除

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
    itemPicker.hidden = false
    return false
}
祝你好运!

答案 3 :(得分:0)

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

您只需在func textFieldShouldBeginEditing中将false更改为true。