Kari Fox
Kari Fox

Reputation: 59

Picker not outputting row data swift 3

I'm trying to write a color picker for a task app. When I try to add the task the picker never makes the changes to my 'picked' variable (declared on line 5) and will only print "blank" (line 7). I'm new enough to pickers that I can't see what I'm doing wrong. I'm fairly certain it has to do with the setup of my didSelectRow function (last function), because if the function were firing, it would still set something in the else statement, even if it's not the string I want, but it's not making any changes at all.

class SecondViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

    @IBOutlet weak var input: UITextField!

    @IBOutlet weak var picker: UIPickerView!

    var pickerData: [String] = [String]()

    var picked: String = "blank"

    @IBAction func addItem(_ sender: Any) {
        print (picked)
        if (input.text != "" && picked != "") {
            list.append(input.text! as AnyObject)
            if picked == "Red" {
                colors.append(UIColor.red)
            }
            else if picked == "Orange" {
                colors.append(UIColor.orange)
            }
            else if picked == "Yellow" {
                colors.append(UIColor.yellow)
            }
            else if picked == "Green" {
                colors.append(UIColor.green)
            }
            else if picked == "Blue" {
                colors.append(UIColor.blue)
            }
            else if picked == "Purple" {
                colors.append(UIColor.purple)
            }
            else if picked == "White" {
                colors.append(UIColor.white)
            }
            input.text = ""
            userDefaults.set(list, forKey: "tasks")
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
        self.picker.delegate = self
        self.picker.dataSource = self

        pickerData = ["Select a color", "White", "Red", "Orange", "Yellow", "Green", "Blue", "Purple"]
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    }

    func numberOfComponents(in 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? {
        return pickerData[row]
    }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if row == 0 {
            picked = ""
        }
        else {
            picked = pickerData[row]
        }
    }
}

Upvotes: 0

Views: 22

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

You forget argument label _

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

Upvotes: 1

Related Questions