Search code examples
iosuinavigationcontrollerstoryboardpushviewcontroller

iOS pushViewController is not working


I have a navigation controller, a view controller, and a table view that were all created in my storyboard:

enter image description here

When you touch a certain row in the table view, it SHOULD push a new controller onto the navigation controller, but instead nothing happens. Here is the code:

enter image description here

- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];

UINavigationController *controller = (UINavigationController*)[mainStoryboard
                                                               instantiateViewControllerWithIdentifier: @"dbNav"];

SubLevelViewController *slController = [[SubLevelViewController alloc] initWithStyle:UITableViewStylePlain];
[controller pushViewController: slController animated: YES];
}

Solution

  • Try

    - (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
    
        UINavigationController *controller  = self.navigationController;
    
        SubLevelViewController *slController = [[SubLevelViewController alloc] initWithStyle:UITableViewStylePlain];
        [controller pushViewController: slController animated: YES];
     }
    

    Basically instantiateViewControllerWithIdentifier creates a new instance of the specified view controller each time you call it, so you are pushing the view in a different Navigation controller than the one that is displayed