views:

869

answers:

2

I have an UIViewController, this controller is contained in a navigationController. I add an UITableViewController in this viewController. I would like to call a pushViewController method when I press on a cell of my tableView.

I tried this :

UITableViewController


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{   
  FirstView *myViewController = [[FirstView alloc] init];
  [f pushIt];
}

UIViewController (FirstView)


-(void)pushIt
{
    SecondView *sCont = [[SecondView alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:sCont animated:YES];
    NSLog(@"didSelect"); // is printed
    [sCont release];
    sCont = nil;
}

But nothing happen. I put NSLog() to my pushIt method and I can see it. So I don't understand why I can't push it.

Any idea?

+1  A: 

UIViewController has a property named navigationController that will return a UINavigationController if one exists for the view controller its called from.

Using this property, you can push view controllers onto the navigation stack from your table view's didSelectRowAtIndexPath: method.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    SecondView *sCont = [[SecondView alloc] initWithNibName:@"SecondView" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:sCont animated:YES];
    [sCont release];
}

The reason your current code isn't working is probably because of the following:

  • You already have an instance of FirstViewController, as you said you have added a table view as its subview
  • You try and create a new instance of a FirstViewController when the user taps a cell, which isn't on the navigation stack, so trying to push a view controller onto the stack from there doesn't work, because the navigationController property returns nil.
Jasarien
Thanks for your answers. Jasarien, I put your code but again nothing happens. It's weird ...
Pierre
That's because your table view controller is not part of the navigation stack. It's view is only added as a subview of your FirstViewController's view. Is there a reason why the table view controller is not the root view controller? Does your FirstViewController contain UI other than the table view?
Jasarien
Hum no but I would like to customize my View just to fixe element and make for example : table.view.frame = CGRectMake(20,20,300,300);And customize all elements around it (images for example).
Pierre
Then, I think, perhaps the best approach would be to use delegation. Have your root view controller implement a delegate protocol which can be called from the table view controller when a cell is tapped. You can pass the index path of the table cell for context, and then you can create the appropriate view controller at the root view controller level and push it onto the nav stack. Though it would be much simpler if you set the table view controller to be the root view controller.
Jasarien
Ok thanks a lot, I will try your solution ;)
Pierre
A: 

You alloc and init myViewController but never push it to navigation or window or whatever else, then you push sCont to myViewController, that isn't present at window. First, try not using myViewController, next try to push myViewController to navigation before pushing sCont into it.

Yuriy Zhyromskiy