Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant call a segue in code (Swift)

I have a few views embedded in a navigation controller.

I've created a few segues (some from buttons to other view controllers, some from one view controller to another one.

for example I have a segue (Show) from one button to a view controller called name2, when I press the button it works great, but when I try to call it programmatically the app crashes and gives an error.

This is how I call the segue on ViewController1

self.performSegueWithIdentifier("name2", sender: self)

this is from ViewController1 to ViewController2

I also have a segue that is connected not to a button but from vc1 to vc2, gives the same error.

This is the strange error I get:

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (ViewController2) has no segue with identifier 'name2''

I tried cleaning the product and also reseting the simulator.

A screen shot of my storyboard

storyboard

A screen of the identity inspector of viewcontroller1 (it is actually called ViewController in my project)

Identity Inspector

A screen shot of the segue (name3) segue

The options of the segue : enter image description here

Edit: I found that when I connect a button as an IBAction and then call performSegueWithIdentifier("LogIn", sender: self) from the button it works correctly.

Any help?

like image 791
Eli Braginskiy Avatar asked Sep 24 '14 18:09

Eli Braginskiy


People also ask

How do I pass data with performSegueWithIdentifier?

The performSegueWithIdentifier method takes two arguments, 1. the segue identifier, 2. the parameter you are passing which is of type AnyObject? Then in the prepareForSegue method, you check the segue identifier and cast the sender parameter to the type you passed in earlier.


2 Answers

Do not connect your Button in Storyboard Editor to the other segue (when you want to call the performWithSegue function within your ViewController1) - just connect the first ViewController to the second One.

Then you can use

self.performSegueWithIdentifier("name2", sender: self)

within your ViewController1 class.

like image 64
derdida Avatar answered Sep 29 '22 01:09

derdida


When you set the Segue from one view controller to the other, ensure that you have correctly set the class - see image:

enter image description here

UIStoryBoardSegue was what mine needed.

like image 26
Phil Hudson Avatar answered Sep 29 '22 01:09

Phil Hudson