Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Objective-C.Programming.pdf
Скачиваний:
14
Добавлен:
21.02.2016
Размер:
8.64 Mб
Скачать

Adding new tasks

Adding new tasks

When you created the UIButton instance in application:didFinishLaunchingWithOptions:, you gave it a target/action pair:

[insertButton addTarget:self action:@selector(addTask:)

forControlEvents:UIControlEventTouchUpInside];

The target is self, and the action is addTask:. So the Insert button sends the BNRAppDelegate the addTask: message. Thus, we need to implement the addTask: method in BNRAppDelegate.m.

- (void)addTask:(id)sender

{

// Get the to-do item

NSString *t = [taskField text];

//Quit here if taskField is empty if ([t isEqualToString:@""]) {

return;

}

//Add it to our working array [tasks addObject:t];

//Refresh the table so that the new item shows up [taskTable reloadData];

//And clear out the text field

[taskField setText:@""]; // Dismiss the keyboard

[taskField resignFirstResponder];

}

What’s this resignFirstResponder business? Here’s the short version:

Some view objects are also controls – views that the user can interact with. Buttons, sliders, and text fields are examples of controls. When there are controls on the screen, one of them can be the first responder. Having first responder status means that the control gets dibs on handling any text input from the keyboard or any shake events (such as “Shake to Undo”).

When the user taps a control that can accept first responder status, that control is sent the becomeFirstResponder message. Until another control becomes the first responder or the current control is sent the resignFirstResponder message, that control will keep this status and receive keyboard and shake input.

When a text input control (like a text field) becomes the first responder, the keyboard materializes on the screen. As long as the current first responder is a control that accepts text input, the keyboard will remain on the screen. At the end of addTask:, we tell the text field to resign its status, which causes the keyboard to dematerialize.

Build and run the application. Now you can add tasks!

Saving task data

There is one final feature that you’ll add to iTahDoodle. Naturally, when users quit the app, they’d like their to-do lists to stick around for later.

189

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