Jeremy Griffin
Jeremy Griffin

Reputation: 11

beginner with Swift with an error UIPicker

I am building a simple app that has a PickerView with 3 selections that should link to 3 viewControllers each named FirstViewController, SecondViewController and ThirdViewController.
But I am getting this error

> NSInvalidArgumentException', reason: 'Receiver (<Scroller.ViewController: 0x7ff7b5e0b670>) has no segue with identifier 'SecondSegue'

I have three options in the picker and a button to go to that selection's viewController:

enter image description here

My code below works on first loading then crashes

import UIKit


class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

    @IBAction func continueButtonPressed(_ sender: AnyObject) {
        self.performSegue(withIdentifier: "\(chosenState)Segue", sender: nil)
        print("no chance")
    }
    @IBOutlet weak var pickerView: UIPickerView!

    var pickerData = ["First","Second","Third"]
    var chosenState = ""

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

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

    // The number of columns of data


    // The number of rows of data
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickerData.count
    }

    // The data to return for the row and component (column) that's being passed in
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return pickerData[row]
    }
    //Called when the user changes the selection...
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        chosenState = pickerData[row]
    }

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

can anyone help me understand how to fix this ?

Have I named the Segues ok? They are all named the same - is that correct?

Upvotes: 0

Views: 62

Answers (2)

Zaphod
Zaphod

Reputation: 7260

It seems your SecondSegue to your second UIViewController has not been named properly.

Be careful, it is case sensitive, it must be named precisely SecondSegue. And after having changed the name, don't forget to validate your input with the return key.

Upvotes: 0

DionizB
DionizB

Reputation: 1507

You have to set the segue identifier to second view controller as SecondSegue

Upvotes: 1

Related Questions