Cyogenos
Cyogenos

Reputation: 3719

Create a segue and attach it between two view controllers through Swift?

How would we create a segue in swift programatically, without using Storyboards. I cannot use storyboards because of how the app is structured, so I need to know how to do it programatically.

Essentially, all I want to do is create a segue, and attach it between the source and the destination.

So far, I have created a RollSegue class which is a subclass of UIStoryboardSegue. Now, I'm trying to do the following:

var seg: RollSegue = UIStoryboardSegue("roll", self, destination)

and I have also tried:

var seg: UIStoryboardSegue = UIStoryboardSegue("roll", self, destination)

These lines of code run fine, but when I try to call the performSegue method, I get the error:

Receiver (<Home1: 0x7fc84b573fd0>) has no segue with identifier 'roll'

Upvotes: 2

Views: 307

Answers (1)

JZAU
JZAU

Reputation: 3567

Actually, you can only set segue identifier on storyboard. So if you want to navigate between two controllers, you should use

func pushViewController(_ viewController: UIViewController, animated animated: Bool)

or

func presentViewController(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion completion: (() -> Void)?)

Upvotes: 2

Related Questions