performSegueWithIdentifier from TableViewController to second TVC embedded in NavController not working

19,036

DetailsTVC *detailsTVC = [segue destinationViewController];

That line is incorrect. Since your second TVC is now embedded in a navigation controller, [segue destinationViewController] is now a UINavigationController. This should work:

DetailsTVC *detailsTVC = [[segue destinationViewController] visibleViewController];

Share:
19,036
ElasticThoughts
Author by

ElasticThoughts

Updated on June 11, 2022

Comments

  • ElasticThoughts
    ElasticThoughts almost 2 years

    I have two TableViewControllers with a segue in-between. When a user clicks on a cell in the first TVC they get presented with the second TVC. The segue is modal, has an identifier called "segueToLocationDetails" and passes an object along with it. You can think of the second TVC as a "details" page more or less.

    My code works perfectly in the scenario that I've described above. It breaks however as soon as I embed the second TVC into a navigation controller.

    Example. I have it working perfectly. I then highlight the second TVC in IB, mouse over to Product | Embed in | Navigation controller. Now the second TVC is in a Nav Controller. The segue however still points to the second TVC. I remove the segue and reconnect it from the first TVC's cell to the Navigation Controller and be sure give the segue an identifier. Run again, and it breaks! The error is below...

    2011-12-23 15:30:45.469 Project12[5219:11603] -[UINavigationController setDetailsObject:]: unrecognized selector sent to instance 0x7b92ce0 2011-12-23 15:30:45.471 Project12[5219:11603] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setDetailsObject:]: unrecognized selector sent to instance 0x7b92ce0' * First throw call stack: (0x16ea052 0x150ad0a 0x16ebced 0x1650f00 0x1650ce2 0x3933 0x703e1e 0x36f6d9 0x36f952 0xbf786d 0x16be966 0x16be407 0x16217c0 0x1620db4 0x1620ccb 0x14ec879 0x14ec93e 0x2dfa9b 0x2a98 0x29f5 0x1) terminate called throwing an exceptionCurrent language: auto; currently objective-c

    Some code is below to help explain:

    AllLocations.h & AllLocations.m (this is the master table)

    AllLocations.h
    
    @interface AllLocations : UITableViewController
    {
        SQLiteDB *mySQLiteDB;
    }
    @property (nonatomic, strong) NSMutableArray *locationsArray;
    
    
    
    AllLocations.m
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [self performSegueWithIdentifier:@"segueToLocationDetails" sender:self];
    }
    
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([[segue identifier] isEqualToString:@"segueToLocationDetails"]) 
        {
            NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
            NSInteger rowNumber = selectedIndexPath.row;
    
            mySQLiteDB = (SQLiteDB *) [locationsArray objectAtIndex:rowNumber];
    
            DetailsTVC *detailsTVC = [segue destinationViewController];
    
            detailsTVC.detailsObject = mySQLiteDB;        
        }
    }
    

    DetailsTVC.h & DetailsTVC.m (this is the detailed table view)

    DetailsTVC.h
    
    @interface DetailsTVC : UITableViewController
    
    @property (nonatomic, strong) SQLiteDB *detailsObject;
    
    
    DetailsTVC.m
    
    @implementation SpotDetailsTVC
    
    @synthesize spotDetailsObject;
    

    Note: I left out all of the code that was not really important or relevant to the question.

    Again: This works perfectly if the segue goes from the Originating TableVeiwController to the other TableViewController. It only breaks when I embed the second TVC in a Nav Controller. I need to know how to get this working with the Nav Controller in the picture. Thanks in advance!

  • ElasticThoughts
    ElasticThoughts over 12 years
    Ok, thank you, however I'm getting this warning now:file://localhost/Users/me/Desktop/Project13/AllLocations‌​.m: warning: Semantic Issue: Incompatible pointer types initializing 'locationDetailsTVC *__strong' with an expression of type 'UIViewController *'
  • Scott Berrevoets
    Scott Berrevoets over 12 years
    Try this in prepareForSegue:sender: to find out what destionationController is: NSLog(@"%@", NSStringFromClass([[segue destinationViewController] class]))
  • ElasticThoughts
    ElasticThoughts over 12 years
    2011-12-23 16:32:20.741 Project13[5452:11603] UINavigationController
  • Scott Berrevoets
    Scott Berrevoets over 12 years
    Maybe you need to cast that variable, as the sender is an id. Not really sure what that warning means.
  • bashan
    bashan over 10 years
    This also works for me, but now I get this exception: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported'. Any way of working around this issue?
  • Scott Berrevoets
    Scott Berrevoets over 10 years
    @bashan: You can't push a navigation controller onto another navigation controller. You can probably display it modally, but also consider taking out the second navigation controller altogether.
  • bashan
    bashan over 10 years
    That's the whole point: I never pushed a navigation controller, but I still get it in the "[segue destinationViewController]". I have a tab bar application with 2 tabs (for the example). Each tab is a navigation controller of its own with few view controllers (pushed). There is a case in which I push a view controller of one tab to a view controller of another tab. The thing is that some times I get the view controller properly and some times I get the navigation controller. Any idea how did the navigation controller got there?