Search code examples
iosswiftxcodeuiviewcontrollersegue

How do I use performSegue to get to another UIViewController?


I tried to connect my ViewController to a CollectionViewController with a segue between the two. I control+clicked from the ViewController to the CollectionView Controller and selected the 'show detail' segue, and then made a button with this code:

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    print("ViewController loaded")
}

@IBAction func EditCharacterSetPressed(_ sender: UIButton) {
    self.performSegue(withIdentifier: "CollectionViewSegue", sender: self)
}

When I press the button in the simulator, it gives me this error message:

unrecognized selector sent to instance

How do I fix this? Does it have something to do with these other warnings about my CollectionView?:

prototype collection view cells must have reuse identifiers

I have an Identifier, so that's not the problem:

Storyboard segue - Identifier: CollectionViewSegue


Solution

  • EDIT: Problem was, he had non-existent IBAction connected to his button.

    It seems like your segue does not have an identifier in your storyboard. Based on your code, it should be

    CollectionViewSegue
    

    segue-name

    Regarding your collection view cells. If I recall correctly (did objective-c couple of years back) xcode is creating instances of new Cell and not re-using any of the existing, because you don't have prototype cells defined.

    You can (and should) create a cell (either in storyboard, or purely in code), set an identifier to that cell and reuse it in your table view controllers (or in this case in your collection view).

    I guess you are just starting with the development, so I would suggest to go through couple of tutorials (e.g. http://www.thomashanning.com/uitableview-tutorial-for-beginners/)