Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how create a segue from view controller to itself

Tags:

xcode

ios

swift

My app have categories and this categories can have infinite subcategories or videos. My principal View controller have a uicollection vith cells. I need that that when i select a category, if the category have subcatehories then create other view controller like this the first with subcategories, but if the category have videos go to other different view controller.

i think that i need two segues from my cells in my principal view controller:

  • one of them wit destination to show videos view controller
  • other go to it self for show all the subcategories.

I want to drag a segue from my cell of my voew controller, to itself. So I can push "infinite" instances of that particular view controller(for all possible subcategories).

But i dont know how drag a segue from view controller to itself. when i try to drag a second segue xcode deleted my first segue.

thanks you friends.

like image 763
BarneyL. BarStin Avatar asked Jul 27 '15 07:07

BarneyL. BarStin


3 Answers

just create custom segue from the code. You aren't then limited to only one segue like when using storyboards.

 UIViewController *toViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"OtherViewControllerId"];
 MyCustomSegue *segue = [[MyCustomSegue alloc] initWithIdentifier:@"" source:self destination:toViewController];
 [self prepareForSegue:segue sender:sender];
 [segue perform];

or create a view controller with ID and push it

UIViewController *myController = [self.storyboard instantiateViewControllerWithIdentifier:@"MyController"];
[self.navigationController pushViewController: myController animated:YES];
like image 156
Mobile Developer Avatar answered Sep 28 '22 07:09

Mobile Developer


Here is an answer in Swift based on @MaciekWrocław

let destinationViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DestinationViewController")

let segue = UIStoryboardSegue(identifier: "SegueToProfileViewController",
                                      source: self,
                                      destination: anotherProfileVC,
                                      performHandler: {
                                                self.navigationController?.show(anotherProfileVC, sender: self)
        })

        self.prepare(for: segue, sender: user)
sege.perform()
like image 33
Chi-Hwa Michael Ting Avatar answered Sep 28 '22 08:09

Chi-Hwa Michael Ting


Swift 4:

First, add this class extension on UIViewController somewhere in your codebase:

extension UIViewController {
    class func storyboardInstance(storyboardId: String, restorationId: String) -> UIViewController {
        let storyboard = UIStoryboard(name: storyboardId, bundle: nil)
        return storyboard.instantiateViewController(withIdentifier: restorationId)
    }
}

Then instantiate an instance of YourViewController class (using the extension above), and push to it from your navigationController:

let destinationVC = YourViewController.storyboardInstance(storyboardId: "YourStoryboardName", restorationId: "idYouSetInStoryboard") as! YourViewController
self.navigationController?.pushViewController(destinationVC, animated: true)
like image 25
vikzilla Avatar answered Sep 28 '22 07:09

vikzilla