BarneyL. BarStin
BarneyL. BarStin

Reputation: 343

how create a segue from view controller to itself

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:

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.

Upvotes: 10

Views: 4258

Answers (4)

vikzilla
vikzilla

Reputation: 4138

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)

Upvotes: 1

Chi-Hwa Michael Ting
Chi-Hwa Michael Ting

Reputation: 179

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

Upvotes: 1

Greg
Greg

Reputation: 25459

You need to remove the segue and add the code to create VC and push it when cell is tapped in didSelectRowAtIndexPath:

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!){

    // Create your view controller and push it
    // pass data if needed
    self.navigationController.pushViewController(yourViewController, animated: YES);
}

Upvotes: 0

Mobile Developer
Mobile Developer

Reputation: 5760

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];

Upvotes: 7

Related Questions