Multiple UIPickerView in one ViewController

情到浓时终转凉″ 提交于 2019-12-25 05:43:08

问题


I have two UiTextfield which are supposed to show different UiPickerView when clicked on a respective UiTextfield....... so i used this if(textField.isFirstResponder() == true) to find which textfield has been clicked...this does not work

class ViewController: UIViewController, UIPickerViewDataSource , UIPickerViewDelegate {

var pickerData  = ["11", "12", "13"]
var pickerDataOthr = ["ada","daad","ada","daad","ada","daad","ada","daad"]
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var textFieldTwo: UITextField!
@IBOutlet weak var tftw: UITextField!



func pickerCode(){

    let picker: UIPickerView
    picker = UIPickerView(frame: CGRectMake(0, 200, view.frame.width, 300))
    picker.backgroundColor = .whiteColor()

    picker.showsSelectionIndicator = true
    picker.delegate = self
    picker.dataSource = self

    let toolBar = UIToolbar()
    toolBar.barStyle = UIBarStyle.Default
    toolBar.translucent = true
    toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
    toolBar.sizeToFit()

    let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "donePicker")
    let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
    let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "donePicker")

    toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
    toolBar.userInteractionEnabled = true

}

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

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
    var count: Int
    if(textField.isFirstResponder() == true)
    {
        count = pickerData.count
    }
    else
    {
        count = pickerDataOthr.count
    }
    return count
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    var stro:String
    if(textField.isFirstResponder() == true)
    {
        stro = pickerData[row]
    }
    else
    {
        stro = pickerDataOthr[row]
    }
    return stro
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if(textField.isFirstResponder() == true)
    {
         textField.text = pickerData[row]
    }
    else
    {
        textFieldTwo.text = pickerDataOthr[row]
    }

}

func donePicker(){
    if(textField.isFirstResponder() == true)
    {
        textField.resignFirstResponder()
    }
    else
    {
        textFieldTwo.resignFirstResponder()
    }

}

func canclePicker(){
    if(textField.isFirstResponder() == true)
    {
        textField.resignFirstResponder()
    }
    else
    {
        textFieldTwo.resignFirstResponder()
    }
}

}

回答1:


Adding a tag to every textfield

 var tagMaster: Int!

 func setUpTag(){
   textField.tag = 1
   textField.delegate = self
   textFieldtwo.tag = 2
   textFieldtwo.delegate = self
 }

and in the picker helper methods idetify the clicked picker by the picker tags. In place of

 textField.isFirstResponder()

i used textField.tag



来源:https://stackoverflow.com/questions/33068404/multiple-uipickerview-in-one-viewcontroller

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