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 ?
If you're using dynamic cell prototypes, you obviously can do a segue from the table view cell to the controller without any problem.
When you make your segue, you end up with:
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:
As pointed out by Chris, if only supporting iOS 6 and above, use the above technique, but (a) make sure your
didSelectRowAtIndexPathusesselffor thesenderand then haveshouldPerformSegueWithIdentifieronly allow the segue if thesender == self(thus when thesenderis the table view cell, it will be canceled);Perhaps even easier, just don't define a segue at all and have your
didSelectRowAtIndexPathmanuallyinstantiateViewControllerWithIdentifierand then push/present that view controller as appropriate; orYou 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:
Now you can make a segue from that button to your view controller:
And thus, you end up with the self-referential segue again:
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.
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.
来源:https://stackoverflow.com/questions/13539227/segue-to-current-view-uitableview