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

CHAPTER 9: Navigation Controllers and Table Views

347

All that’s left now are the two text field delegate methods. The first one we implement, textFieldDidBeginEditing:, is called whenever a text field for which we are the delegate becomes first responder. So, if the user taps a field and the keyboard pops up, we get notified. In this method, we store a pointer to the field currently being edited so that we have a way to get to the last changes made before the Save button was tapped.

- (void)textFieldDidBeginEditing:(UITextField *)textField { self.currentTextField = textField;

}

The last method we wrote is called when the user stops editing a text field by tapping a different text field or pressing the Done button, or when another field became the first responder, which will happen, for example, when the user navigates back up to the list of presidents. Here, we save the value from that field in the tempValues dictionary so that we will have the changes if the user taps the Save button to confirm the changes.

- (void)textFieldDidEndEditing:(UITextField *)textField { NSNumber *tagAsNum = [NSNumber numberWithInt:textField.tag]; [tempValues setObject:textField.text forKey:tagAsNum];

}

And that’s it. We’re finished with these two view controllers.

Adding an Editable Detail View Controller Instance

Now, all we need to do is add an instance of this class to the top-level view controller. You know how to do this by now. Single-click BIDFirstLevelController.m.

First, import the header from the new second-level view by adding the following line of code directly before the @implementation declaration:

#import "BIDPresidentsViewController.h"

Then add the following code to the viewDidLoad method:

- (void)viewDidLoad { [super viewDidLoad];

self.title = @"Top Level";

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

// Disclosure Button

BIDDisclosureButtonController *BIDDisclosureButtonController = [[BIDDisclosureButtonController alloc]

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

imageNamed:@"BIDDisclosureButtonControllerIcon.png"]; [array addObject:BIDDisclosureButtonController];

// Checklist

BIDCheckListController *checkListController = [[BIDCheckListController alloc] initWithStyle:UITableViewStylePlain];

checkListController.title = @"Check One"; checkListController.rowImage = [UIImage

imageNamed:@"checkmarkControllerIcon.png"];

www.it-ebooks.info

348 CHAPTER 9: Navigation Controllers and Table Views

[array addObject:checkListController];

// Table Row Controls

RowControlsController *rowControlsController = [[RowControlsController alloc] initWithStyle:UITableViewStylePlain];

rowControlsController.title = @"Row Controls"; rowControlsController.rowImage =

[UIImage imageNamed:@"rowControlsIcon.png"]; [array addObject:rowControlsController];

// Move Me

BIDMoveMeController *moveMeController = [[BIDMoveMeController alloc] initWithStyle:UITableViewStylePlain];

moveMeController.title = @"Move Me";

moveMeController.rowImage = [UIImage imageNamed:@"moveMeIcon.png"]; [array addObject:moveMeController];

[moveMeController release];

// Delete Me

BIDDeleteMeController *deleteMeController = [[BIDDeleteMeController alloc] initWithStyle:UITableViewStylePlain];

deleteMeController.title = @"Delete Me";

deleteMeController.rowImage = [UIImage imageNamed:@"deleteMeIcon.png"]; [array addObject:deleteMeController];

// BIDPresident View/Edit

BIDPresidentsViewController *presidentsViewController = [[BIDPresidentsViewController alloc] initWithStyle:UITableViewStylePlain];

presidentsViewController.title = @"Detail Edit"; presidentsViewController.rowImage = [UIImage imageNamed:

@"detailEditIcon.png"];

[array addObject:presidentsViewController];

self.controllers = array;

}

Save everything, sigh deeply, hold your breath, and then build that sucker. If everything is in order, the simulator will launch, and a sixth and final row will appear, just like the one in Figure 9–2. If you click the new row, you’ll be taken to a list of US presidents (see Figure 9–22).

www.it-ebooks.info

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