views:

808

answers:

2

I have an application with multiple views. I can click on different buttons to select different views, one of which displays a table. When I select a cell on that row, I want to display a new view. I see a log line indicating that view was loaded, but the screen doesn't change.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *targetCustomCell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];

  [tableView deselectRowAtIndexPath:indexPath animated:YES];
  NSLog(@"in didSelectRowAtIndexPath");

  ItemDetailsViewController *detailsController = [[ItemDetailsViewController alloc] initWithNibName:@"ItemDetailsView" bundle:nil];
  [self.navigationController pushViewController:detailsController animated:YES];
  [self.window addSubview:[detailsController view]];
  [detailsController release];
  detailsController = nil;
}

In the ItemDetailsViewController, I have

- (void)viewDidLoad {
  [super viewDidLoad];
  NSLog(@"in ItemDetailsViewController.viewDidLoad ");
}

I see both log lines, but the screen doesn't change. Why isn't the view changing? Is there some other way to change the view?

+3  A: 

Hey Tai,

What happens if you try to present your details controller using [self presentModalViewController: detailsController animated:YES] instead of the navigation controller's pushViewController? Also, adding the view to the window shouldn't be necessary. Pushing the view controller should be sufficient.

Try adding NSLog([self.navigationController description]) and see if self.navigationController is nil. (Might happen if your current controller isn't a sub-controller of a UINavigationController). Passing a message to a nil object just silently fails, so there's a good chance that's the problem.

Hope that helps!

EDIT: It's usually not necessary to subclass UINavigationController. Check out this page in Apple's docs: Apple Docs: Using Navigation Controllers It explains the setup pretty well. If you're creating your "root" view controller programatically in your application delegate, you'd want to do something like this:

// set up main view controller
DrawingBrowserController *controller = [[DrawingBrowserController alloc] init];

// create a navigation controller that "wraps" the controller
navigationController = [[UINavigationController alloc] initWithRootViewController: controller];

[controller release];

// Add the navigation controller's view to the window
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];

Basically, you're "wrapping" your root view controller in a navigation controller. If you're using Interface Builder, you can also set things up there.

Ben Gotow
Using the presentModalViewController worked - based on a previous question (http://stackoverflow.com/questions/948874/) I had one controller that extended UIViewController, not UINavigationController. Do I have to create another controller, or should I add the UINavigationController to the app delegate and access it from there?
Tai Squared
A: 

Did you make sure your navigation controller is the root view of your window? You usually should do it like this in the applicationDidFinishLaunching method:

[window addSubview:navigationController.view];    
[window makeKeyAndVisible];

Additionally, you should remove [self.window addSubview:[detailsController view]];, as it is not necessary.

henning77