简体   繁体   English

当选择了另一个文本字段时,如何重置UIPickerView内部的值?

[英]How to reset the values inside the UIPickerView when the other textfield is selected?

I have a problem regarding in the UIPickerView. 我在UIPickerView中有问题。 When I select textField1 the UIPickerView is correct but when i click to textField2 the value in pickerData1 is displayed in the UIPickerView instead of the value of pickerData2. 当我选择textField1时,UIPickerView是正确的,但是当我单击到textField2时,pickerData1中的值将显示在UIPickerView中,而不是pickerData2的值。 Please help me find the bug or correct my wrong code if it is wrong. 请帮助我找到错误,或者如果错误,请更正我的错误代码。 thanks 谢谢

NOTE: the bool in the didSelectRow and titleForRow function is just a sample. 注意: didSelectRowtitleForRow函数中的bool只是一个示例。 thanks 谢谢

 class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {


    @IBOutlet var textField1: UITextField!

    let pickerData1 = ["11", "12", "13"]

    let pickerData2 = ["14", "15", "16"]



    override func viewDidLoad() {
        super.viewDidLoad()
           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

        textField1.inputView = picker
        textField1.inputAccessoryView = toolBar


       textField2.inputView = picker
       textField2.inputAccessoryView = toolBar
    }

    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? {
        if textfield1selected // just a sample bool
        {
         return pickerData1[row]
        }
        else   // sample bool also
         {
          return pickerData2[row]
         }
    }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

       if textfield1selected // just a sample bool
        {
          textField1.text = pickerData1[row]
        }
        else   // sample bool also
         {
           textField2.text = pickerData2[row]
         }
    }

    func donePicker() {

        textField1.resignFirstResponder()
         textField2.resignFirstResponder()
    }




    }
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

       if textfield1selected // just a sample bool
        {
          textField1.text = pickerData1[row]

          picker.reloadAllComponents();

        }
        else   // sample bool also
         {
           textField2.text = pickerData2[row]
            picker.reloadAllComponents();
         }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM