0

I hope the title makes some sense but what I am trying to do is to set a field value to the item selected variable from my dataPicker. I have been able to make this work when there is only one field to set but my project will have multiple fields on each view that will call data from the dataPicker based on what field it is. I hope that is clear. Maybe as you look at the code it will.

I have set up a test project to limit things to this issue only. So my variable to tell the view what array to populate in the dataPicker is either season or sport. the field that will receive the data from the season/sport array is enterSeason and enterSport. When the picker has returned a value from season, I want to combine it with enter to create the var enterSeason to set that == itemSelected. This language is very new to me so I am trying the only way I have used before to combine text and variables in one value. It is obviously not working. Help is appreciated.

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UIGestureRecognizerDelegate {

    @IBOutlet var enterSeason: UITextField!

    @IBOutlet var enterSport: UITextField!

    var dataPickerView = UIPickerView()
    var season = ["2013", "2014", "2015"] //multi-season
    //var season = ["2015"] //single-season
    var sport = ["Baeball", "Football", "Basketball", "Hokey"]
    var activeField = []

    override func viewDidLoad() {
        super.viewDidLoad()

        enterSeason.inputView = dataPickerView
        dataPickerView.delegate = self
        dataPickerView.dataSource = self

    }

    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 activeField.count
    }
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        return activeField[row] as! String
    }
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        var itemSelected = activeField[row] as! String
        self.enter"\activeField".text = itemSelected
    }
}

1 Answer 1

1

EDIT : How do you show and hide the picker? Your code anly shows variable declarations and the delegate methods... answers could vary accordingly..

Since you show the picker as text field's input view, set UITextFieldDelegate for each of these text fields .. and in the textFieldDidBeginEditing check which field becomes active with simple if else

func textFieldDidBeginEditing(textField: UITextField) {
    if textField === enterSeason {
        activeField = season
    }
    else if textField === enterSport {
        activeField = sport
    }
}

And in the picker selector, set value of the relevant text field as per current activeField object

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if activeField === season {
        enterSeason.text = season[row] as! String
    }
    else if activeField === sport {
        enterSeason.text = sport[row] as! String
    }
}

Setting the delegate for your text fields in storboard/xib :

enter image description here

P.S. - Rename activeField to activeDataArray or somethiong more appropriate

EDIT 2 : As you mentioned, second approach i have mentioned below is not suitable for you because there are too many of these fields i am still keeping it as part of the answer as it may help someone else

But what you are trying to achieve is very simple and approach is too convoluted / weird. So heres another way you can implement the whole thing..

The easiest (but still probably not the best) way is to have two instances of the UIPickerView for each field. you can directly check pickerView == seasonPickerView OR pickerView == sportPickerViewin an if else block and do the conditional programming and you wont need the activeField variable..

Sign up to request clarification or add additional context in comments.

11 Comments

@luka I do not have a picker actually in the view, it is created programmatically with the following code enterSeason.inputView = dataPickerView dataPickerView.delegate = self dataPickerView.dataSource = self Then when the field is tapped, the picker appears and is populated with the appropriate data. There will be 3 to 7 of these fields across several vies so the idea of multiple pickers for each field is not a solutions. Is there a way to capture which field is tapped to activate the picker and then I could set the activeField var based on the field tapped?
I have tried your solution and I think it will work but I am not implementing in correctly. I tried to set the UITextFieldDelegate as such class DispenseScreen1ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate { but you said something about setting that for each field. How do I do that? The picker is not populating at all so I am thinking this might be the issue.
Also, in the picker selector, I get an error in my second else if { statement when I say if activeDataArray === sport { enterSport.text = sport[row] as! String. I get an error for only this line that reads "Binary operator '===' cannot be applied to operands of type 'NSArray' and '[String]'. All of the other else if { statements, using the same syntax do not return errors though. Suggestions?
I have added a screenshot to help you understand setting UITextFieldDelegate .. you'll have to do this for every text field.
Thank you for the reference. I will check it out. Learning, learning, learning.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.