Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning iOS5 Development.pdf
Скачиваний:
7
Добавлен:
09.05.2015
Размер:
15.6 Mб
Скачать

300

CHAPTER 9: Navigation Controllers and Table Views

allocated and initialized a new instance of BIDDetailDisclosureController, so we do that next.

if (childController == nil)

childController = [[BIDDisclosureDetailController alloc] initWithNibName:@"BIDDisclosureDetail" bundle:nil];

This gives us a new controller that we can push onto the navigation stack, just as we did earlier in BIDFirstLevelController. Before we push it onto the stack, though, we need to give it some text to display.

childController.title = @"Disclosure Button Pressed";

In this case, we set message to reflect the row whose disclosure button was tapped. We also set the new view’s title based on the selected row.

NSUInteger row = [indexPath row];

NSString *selectedMovie = [list objectAtIndex:row]; NSString *detailMessage = [[NSString alloc]

initWithFormat:@"You pressed the disclosure button for %@.", selectedMovie];

childController.message = detailMessage; childController.title = selectedMovie;

Finally, we push the detail view controller onto the navigation stack.

[self.navigationController pushViewController:childController animated:YES];

And, with that, our first second-level controller is complete, as is our detail controller. The only remaining task is to create an instance of our second-level controller and add it to BIDFirstLevelController’s controllers.

Adding a Disclosure Button Controller Instance

Select BIDFirstLevelController.m. Up at the top of the file, we’ll need to add one line of code to import the header file for our new class. Insert this line directly above the

@implementation declaration:

#import "BIDDisclosureButtonController.h"

Then insert the following code in the viewDidLoad method:

- (void)viewDidLoad { [super viewDidLoad];

self.title = @"First Level";

NSMutableArray *array = [[NSMutableArray alloc] init];

// Disclosure Button

BIDDisclosureButtonController *disclosureButtonController = [[BIDDisclosureButtonController alloc]

initWithStyle:UITableViewStylePlain]; disclosureButtonController.title = @"Disclosure Buttons"; disclosureButtonController.rowImage = [UIImage

imageNamed:@"disclosureButtonControllerIcon.png"]; [array addObject:disclosureButtonController];

www.it-ebooks.info

CHAPTER 9: Navigation Controllers and Table Views

301

self.controllers = array;

}

All that we’re doing is creating a new instance of BIDDisclosureButtonController. We specify UITableViewStylePlain to indicate that we want a normal table, not a grouped table. Next, we set the title and the image to one of the .png files we added to our project, add the controller to the array, and release the controller.

Save your changes, and try building. If everything went as planned, your project should compile and then launch in the simulator. When it comes up, there should be just a single row (see Figure 9–10).

Figure 9–10. Our application after adding the first of six second-level controllers

If you touch the one row, it will take you down to the BIDDisclosureButtonController table view we just implemented (see Figure 9–11).

www.it-ebooks.info

302

CHAPTER 9: Navigation Controllers and Table Views

Figure 9–11. The Disclosure Buttons view

Notice that the title that we set for our controller is now displayed in the navigation bar, and the title of the view controller we were previously using (First Level) is contained in a navigation button. Tapping that button will take you back up to the first level. Select any row in this table, and you will get a gentle reminder that the detail disclosure button is there for drilling down (see Figure 9–12).

www.it-ebooks.info

CHAPTER 9: Navigation Controllers and Table Views

303

Figure 9–12. Selecting the row does not drill down when there is a detail disclosure button visible.

If you touch the detail disclosure button itself, you drill down into the BIDDisclosureDetailController view (see Figure 9–13). This view shows information that we passed into it. Even though this is a simple example, the same basic technique is used anytime you show a detail view.

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]