0
votes

My RootViewController is a UITableViewController. A UINavigationController is added programmatically:

_navigationController = [[[UINavigationController alloc] initWithRootViewController:_rootViewController] autorelease];
[self.window addSubview:_navigationController.view];
[self.window makeKeyAndVisible];

Within the RootViewController.m the DetailViewController should be loaded when a row is selected:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  NSLog(@"Switch to detail");
  CCouchDBDocument *selectedObject = [self.contentsList objectAtIndex:indexPath.row];
  DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
  [self.view addSubview:detailViewController.view];
  [detailViewController setDetailItem: selectedObject]; 
  [self.navigationController pushViewController:detailViewController animated:YES];
  [detailViewController release];
}

Without addSubView nothing happens on the screen. All the examples I've seen before only use pushViewController. And, loading the DetailView takes about 4 seconds. That's way too long (it's currently empty, just one label). When I try to set the navigationTitle (self.title = @"Hello";), the title remains the same from the RootViewController, so something must be wrong with the navigationController.

I tried to put everything in the AppDelegate and use a switchView method. The problem is the call for setDetailItem, which I can't call if I work with the switch method.

What would be the correct way to load the DetailView from the RootViewController into the navigation stack and possibly more from the DetailViewController later?

Update I started from the beginning again with a Window-based application. Added a UITableViewController as "RootViewController" and initialised it with the UINavigationController in the AppDelegate (did absolutely nothing in the XIB). When I try to set self.navigationController.title = @"Test"; in ViewDidLoad, nothing happens.

What's wrong there?

1

1 Answers

1
votes

You don't set the title of the DetailView when it's displayed using a UINavigationController by using self.title, you need to set the UINavigationItem title property in the DetailView initializer.

e.g. in the DetailView initializer :-

self.navigationItem.title = @"Hello";

You're right you shouldn't need to add the detailViewController view as a subview of the current view - you should just need the pushViewController call. I'm not sure why it's not appearing though.

Obvious questions are is everything connected OK in the nib, and what does the DetailView initializer do?