问题
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'
回答1:
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)?)
来源:https://stackoverflow.com/questions/37063674/create-a-segue-and-attach-it-between-two-view-controllers-through-swift