user3925803
user3925803

Reputation: 3295

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];
}

Upvotes: 0

Views: 831

Answers (2)

Waruna
Waruna

Reputation: 162

This worked for me. You have to define index path.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ServicesModel *service = [services objectAtIndex:indexPath.row];
    ServiceViewController *serviceViewController = [self.storyboard        instantiateViewControllerWithIdentifier:@"ServiceView"];
    serviceViewController.serviceModel = service;
    [self.serviceController pushViewController:serviceViewController animated:YES];
}

Upvotes: 0

Y2theZ
Y2theZ

Reputation: 10402

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

Upvotes: 2

Related Questions