UITEXTFIELD InputView Uipicker not working in swift

大兔子大兔子 提交于 2019-12-13 04:56:42

问题


hello I am trying to pop a UIPickerView view programmatically when user clicks on textfield. I have tried this but it doesn't doing anything. it isn't working. nothing is happening when I click the textfield

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.
    }


}

回答1:


The issue in your current code is in this method:

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

I would suggest removing this method entirely.




回答2:


Ok so I have dug out something similar I wrote, It uses a datepicker but the general code is the same.

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             
    }

Then you add in a function to handle the done button press

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

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

Just replace YOURTEXTFIELD with your version genderTxtField and change the date picker to your picker.

Dont forget to check if everything is linked up




回答3:


itemPicker!.backgroundColor = UIColor.blackColor() 

change to

 itemPicker!.backgroundColor = UIColor.whiteColor()

then remove

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

good luck!




回答4:


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

You just have to change false to true in func textFieldShouldBeginEditing.



来源:https://stackoverflow.com/questions/35057684/uitextfield-inputview-uipicker-not-working-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!