Storyboard Segue From View Controller to Itself

34,519

Solution 1

I developed a method to create a segue using a phantom button. I believe it will solve your problem. You can read about it in my answer here.

Solution 2

If you are using a navigation controller you need to push the ViewController into the nav stack. In this example, i named my ViewController "VDI" in my Storyboard ID setting.

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
YourVC *dest = [storyboard instantiateViewControllerWithIdentifier:@"VDI"];
[self.navigationController pushViewController:dest animated:YES];

If you don't want the NavigationController to keep adding itself into your "Back" history you can pop the stack before adding to it like so.

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
YourVC *dest = [storyboard instantiateViewControllerWithIdentifier:@"VDI"];
UINavigationController *navController = self.navigationController;
[navController popViewControllerAnimated:NO];
[navController pushViewController:dest animated:YES];

Solution 3

Using Xcode 5 there is a much simpler solution.

  1. Click the table cell in the storyboard
  2. Open the Connections Inspector (right arrow icon in the upper right)
  3. Under "triggered segues" you see "selection"
  4. Drag from the circle next to "selection" to the cell in the storyboard

That's it.

Solution 4

Instead of performing a segue to the same controller, you can instantiate a view controller (the same one) from storyboard, and then push that onto the navigation controller.

Solution 5

Interface Builder approach: Just segue to a storyboard reference which refers back to the presenting view controller.

Share:
34,519

Related videos on Youtube

Jorge
Author by

Jorge

Updated on July 05, 2022

Comments

  • Jorge
    Jorge almost 2 years

    I am trying to make a mechanism to drill down a file / folder list. The idea is to show the same file list view controller every time the user selects a folder, and show a file detail view controller if he/she selects a file.

    So far, I have created a segue from the file list view controller to the file detail view controller, and a segue from the file list table view cell to the the file list table view controller:

    enter image description here

    The issue with this is that as soon as the user taps the cell, the segue is executed. I would like to remove the segue from the table view cell and make one from the file list view controller to itself. That way, I could trigger the right segue programmatically when the user tapped the cell.

    So, my question is: Is it possible to create a segue from a view controller to itself in Interface Builder?

    • John Henckel
      John Henckel about 10 years
      Yes, using xcode 5 you can. see post.
    • DawnSong
      DawnSong almost 7 years
      Open the Editor menu, click Show Document Outline, then just right-button-drag from the cell to the Outline's File ListViewController object.
  • Jorge
    Jorge over 12 years
    This put me on the right track. I added a button on the "Dock", and connected it to the file list view controller. Now I can perform the segue programmatically as I wanted. Thanks!
  • jogloran
    jogloran almost 12 years
    I wish this weren't necessary just to get a self-segue, but this works.
  • strada
    strada about 11 years
    Mustafa's solution is less hacky and pretty straightforward. Making segues to same views with phantom buttons will only make your storyboard a mess.
  • siburb
    siburb over 10 years
    Both this one and @TJ/@Jorge methods will work. I've gone for this way because it feels cleaner. Benefits that I can see in the other method are that you can see your looping segues in the storyboard, and that you can handle all your navigation in "prepareForSegue:"
  • John Smith
    John Smith almost 10 years
    This approach works like a charm with no line of code.
  • Nerrolken
    Nerrolken almost 10 years
    Worked perfectly! Also, that's the "Storyboard ID" in XCode that he set to "VDI," for those looking to recreate this.
  • Steve Moser
    Steve Moser almost 9 years
    I like the phantom button solution better all my forward transitions go through prepareForSegue: instead of remember if a transition is created in IB or code.
  • CupawnTae
    CupawnTae over 8 years
    This is the correct answer for xcode 5+ and these days you should use a "show" segue for this.
  • db0
    db0 almost 8 years
    Doesn't seem to work for me, the view controller doesn't become "blue" to allow to add the segue.
  • Roland T.
    Roland T. over 6 years
    This approach is the nicest if other navigation is done with segues in IB! Thanks!
  • Christophe Blin
    Christophe Blin over 5 years
    Should definitely be the accepted answer because storyboard reference are now available (which was not the case when op ask the question)
  • ZShock
    ZShock over 5 years
    I found this to be the most elegant solution now that it's available
  • John J. Camilleri
    John J. Camilleri over 5 years
    Extra details: you need to give your controller a Storyboard ID and then use that ID in the Referenced ID field of the reference.