简体   繁体   中英

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:

  • 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.

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

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)

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);
}

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

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