简体   繁体   中英

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'

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)?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM