Cant call a segue in code (Swift)

ぐ巨炮叔叔 提交于 2020-01-12 15:00:36

问题


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

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

A screen shot of the segue (name3)

The options of the segue :

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?


回答1:


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.




回答2:


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

UIStoryBoardSegue was what mine needed.




回答3:


The error says that you're looking for a segue in ViewController2 but your question states that the segue comes from a button. That would explain why it can't be found.




回答4:


Try to remove the segue identifier ( name ), build ( cmd + b ), then set the segue identifier and compile. ( in storyboard of course )




回答5:


Ok the answer is that I'm pretty dumb,
Whenever I was creating new ViewControllers, I was subclassing ViewController which was my first ViewController, instead of UIViewController which made it run recursivly.



来源:https://stackoverflow.com/questions/26023766/cant-call-a-segue-in-code-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!