Many views, same format


#1

I’m working through chapter 12 and I’m trying to adapt the ideas to a project I’m working on. Basically I want to use a navigation controller to navigate through a hierarchy of table views. For example, if the app contained a catalog of books, you might navigate through Fiction > 20th century > American > William Faulkner > The Sound and the Fury.

Each view will have the exact same format, just different content. (Content comes from NSArrays populated by Core Data). Do I need a view controller for each node in this hierarchy, or is there a way for a single view controller to push a new view on with the same format, but updated content? I’m thinking the answer may still be in Chapter 12, but I’m a bit stuck.

Thanks,
Jack


#2

You can create multiple instances of the same UIViewController subclass.

Let’s say each view is intended to show a text string in a label. The view controller subclass would implement loadView (or have a XIB file) where there is a UILabel instance on its view. It would implement a method like setLabelString: that would take a string and set it as the text of the label. Each time you drill down, you would create a new instance of this view controller subclass and give it a string.

@interface DetailedViewController : UIViewController
{
     id data; // whatever this happens to be
}
@property id data;
@end

@implementation DetailedViewController
@synthesize data;
- (void)drillDown
{
     DetailedViewController *nextVC = [[[DetailViewController alloc] init] autorelease];
    [nextVC setData]; 
    [[self navigationController] pushViewController:nextVC animated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear];
   [self configureSubviewsWithData:[self data]];
}
@end

#3

Thanks for the explanation. I see that it works, and I actually finally got my app working, but what still confuses me is the fact that it seems like you’re creating multiple instances of the same view controller with the same name, nextVC in your example. Since a navigation controller holds an array containing all of the view controllers in its stack, I thought that having two instances named nextVC would cause some kind of conflict. I’m glad I was wrong.

As I was researching this, I also found a tutorial that showed how to do exactly what I wanted. Here’s the link:
UITableView - Drill down table view tutorial - iPhone SDK Articles
Hope this helps.

Jack


#4

The name doesn’t matter. Think of it this way: every view controller is its own object. It has its own instance variables. Each of these instance variables has a name. This name only matters to the view controller. The name of the instance variable is what this view controller calls the object pointed to by the variable.

When you put an object into an array, the pointer to that object is stored in the array. The name “nextVC” is something the view controller calls this pointer, but the array has no knowledge of this.