Reputation: 27
I have a pretty simple question. Say you have 2 view controllers A and B. A is a UITableView with STATIC CELLS. That is A is built using storyboard objects only as opposed to programmatically. B is also a UITableView but built programmatically with DYNAMIC CELLS. So I wired up Segues (with identifiers set in the storyboard) from each cells in A to the B Tableview.
Now what I would like to have is to know which segue has been pushed when a row in A is selected. I know this can easily be done if I create the cells in view A programmatically and use the prepare/perform segue methods. But since the contents in A will never change, I do not want to go that route. Reason why I am trying to find out how to check which segue has been pushed when I select a given row in A. Ideally there would be some for of a method DIDPERFORMSEGUE: (Segue identifier) I could call from the B Viewcontroller.
Thanks very much for your help and suggestions.
Upvotes: 1
Views: 545
Reputation: 124997
I can't say I've needed to do this myself, but if you really don't want to change the source view controller or if you have a frequent need to identify the segue from the destination view controller, one way to do it would be to create your own UIStoryboardSegue subclass. A segue already knows about the destination view controller, so it's a simple matter to give the destination a chance to inspect the segue. Something like this should do the trick:
@interface MyStoryboardSegue : UIStoryboardSegue
@end
@implementation MyStoryboardSegue
- (void)perform
{
[super perform]
if ([self.destinationViewController respondsToSelector:@selector(didPerformSegue:)]) {
[self.destinationViewController didPerformSegue:self];
}
}
@end
It's not a great solution if you're already using UIStoryboardSegue subclasses for other reasons. And in fact, I haven't even tested the code, so perhaps there are some snags I haven't thought of yet. The point, however, is that if you want a segue to notify the destination when it executes, you can probably arrange it.
Upvotes: 0
Reputation: 7717
In your "A" TableView, you should be able to peek at which row was poked, and then push that information to your "B" table. For example:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier] isEqualToString:@"UITableView_B"]){
BTableViewController *vc = (BTableViewController *)[segue destinationViewController];
NSIndexPath *path;
path = [self.tableView indexPathForSelectedRow];
[vc setSelectedPath:path];
}
}
Hope that helps. Best of luck.
Edit: Probably obvious, but in the above "self.tableView" is an outlet pointing at the UITableView.
Update Personally, I'd bite the bullet and make a class for "A", but in the interest of hacking - it should be possible to reach back and get data from the previous view. This is 'bad code' (imo) and assumes you're using a Navigation Controller - and that the previous view is an "A" table, etc... Without further ado - is should be possible to just do this:
NSArray * views = [self.navigationController viewControllers];
NSUInteger prevViewIndex = [views count] - 2;
UIViewController * previousView = [views objectAtIndex:prevViewIndex];
ATableViewController * aTableViewController = (ATableViewController *) previousView;
NSIndexPath *path = [aTableViewController.tableView indexPathForSelectedRow];
Upvotes: 2