一个ViewController中有多个UIPickerView

时间:2015-10-11 18:11:20

标签: ios swift uitextfield uipickerview

我有两个UiTextfield,它们应该在点击相应的UiTextfield时显示不同的UiPickerView ....... 所以我用过这个         if(textField.isFirstResponder()== true) 找到哪个文本字段已被点击...这不起作用

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 个答案:

答案 0 :(得分:0)

为每个文本字段添加标记

 var tagMaster: Int!

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

并在picker helper方法中通过picker标签识别单击的选择器。代替

 textField.isFirstResponder()
我用过      textField.tag