2

I have an undefined number of levels (depth) in my tableview, so i'd like to be able to create a segue between the view and itself so that i can call it as long as i have sub levels to discover. How can i do that ?

I've thought of directly using [self.navigationView pushViewController] when i need it, but i'd like the new controller to use the defined view in my SB.

Thanks,

EDIT: For now the only solution i've found is duplicating the VC, setting a segue between VC1 and VC2 called 'showNext', and a segue between VC1 and VC2 called the same way. Isn't there a better solution ?

Cuva
  • 335
  • 1
  • 4
  • 15

2 Answers2

19

If you're using dynamic cell prototypes, you obviously can do a segue from the table view cell to the controller without any problem.

make segue from cell to controller

When you make your segue, you end up with:

circular segue

But let's imagine for a second that there's some reason that doesn't work for you, e.g. you could not have a segue from the cell (for example, you need to invoke segue from didSelectRowAtIndexPath, not upon selecting the cell). In this case, you couldn't use that previous technique.

There are a couple of options in this case:

  1. As pointed out by Chris, if only supporting iOS 6 and above, use the above technique, but (a) make sure your didSelectRowAtIndexPath uses self for the sender and then have shouldPerformSegueWithIdentifier only allow the segue if the sender == self (thus when the sender is the table view cell, it will be canceled);

  2. Perhaps even easier, just don't define a segue at all and have your didSelectRowAtIndexPath manually instantiateViewControllerWithIdentifier and then push/present that view controller as appropriate; or

  3. You can use the following, kludgy work-around: In short, add a button to your view controller (drag it down to the bar at the bottom), where it won't show up on the view, but you can use its segues. So first, drag the rounded button to the controller's bar at the bottom of the scene:

    drag button

    Now you can make a segue from that button to your view controller:

    make segue from hidden button

    And thus, you end up with the self-referential segue again:

    final button segue

    You must give that segue an identifier, so you can invoke it programmatically via performSegueWithIdentifier, but it works. Not the most elegant of solutions, but I think it's better than having an extra scene on your storyboard.

None of these are ideal, but you have lots of options.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Thanx for your complete answer. – Cuva Nov 25 '12 at 09:39
  • Thanks for the illustrated step-by-step. I found a [workaround](http://stackoverflow.com/a/16247976/222458) that doesn't require the hidden button for IOS 6. – Chris Apr 27 '13 at 04:04
  • @Chris Agreed, if you can forgo support for iOS versions prior to 6.0, `shouldPerformSegueWithIdentifier` opens up more options. – Rob Apr 27 '13 at 04:19
0

This process worked for me to do the exact same thing...

Set up your tableview in storyboard as usual. DO NOT give your prototype cell a Cell Identifier. Set up your segue as you did above, by dragging from the cell to the controller.

In your viewDidLoad register the cell class

//register the cell identifier as we are not loading form a storybard
[_aTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CellID"];

In your cellForRowAtIndexPath set up your cell

static NSString *CellIdentifier = @"CellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

Now you can use didSelectRowAtIndexPath and call performSegueWithIdentifier. the prepareForSegue will only be called once.

Ferdinand Rios
  • 972
  • 6
  • 18