Adding new tasks

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

 ​ ​ ​ ​[​i​n​s​e​r​t​B​u​t​t​o​n​ ​a​d​d​T​a​r​g​e​t​:​s​e​l​f​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​a​c​t​i​o​n​:​@​s​e​l​e​c​t​o​r​(​a​d​d​T​a​s​k​:​)​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​f​o​r​C​o​n​t​r​o​l​E​v​e​n​t​s​:​U​I​C​o​n​t​r​o​l​E​v​e​n​t​T​o​u​c​h​U​p​I​n​s​i​d​e​]​;​

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.


-​ ​(​v​o​i​d​)​a​d​d​T​a​s​k​:​(​i​d​)​s​e​n​d​e​r​
{​
 ​ ​ ​ ​/​/​ ​G​e​t​ ​t​h​e​ ​t​o​-​d​o​ ​i​t​e​m​
 ​ ​ ​ ​N​S​S​t​r​i​n​g​ ​*​t​ ​=​ ​[​t​a​s​k​F​i​e​l​d​ ​t​e​x​t​]​;​

 ​ ​ ​ ​/​/​ ​Q​u​i​t​ ​h​e​r​e​ ​i​f​ ​t​a​s​k​F​i​e​l​d​ ​i​s​ ​e​m​p​t​y​
 ​ ​ ​ ​i​f​ ​(​[​t​ ​i​s​E​q​u​a​l​T​o​S​t​r​i​n​g​:​@​"​"​]​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​r​e​t​u​r​n​;​
 ​ ​ ​ ​}​

 ​ ​ ​ ​/​/​ ​A​d​d​ ​i​t​ ​t​o​ ​o​u​r​ ​w​o​r​k​i​n​g​ ​a​r​r​a​y​
 ​ ​ ​ ​[​t​a​s​k​s​ ​a​d​d​O​b​j​e​c​t​:​t​]​;​
 ​ ​ ​ ​/​/​ ​R​e​f​r​e​s​h​ ​t​h​e​ ​t​a​b​l​e​ ​s​o​ ​t​h​a​t​ ​t​h​e​ ​n​e​w​ ​i​t​e​m​ ​s​h​o​w​s​ ​u​p​
 ​ ​ ​ ​[​t​a​s​k​T​a​b​l​e​ ​r​e​l​o​a​d​D​a​t​a​]​;​
 ​ ​ ​ ​/​/​ ​A​n​d​ ​c​l​e​a​r​ ​o​u​t​ ​t​h​e​ ​t​e​x​t​ ​f​i​e​l​d​
 ​ ​ ​ ​[​t​a​s​k​F​i​e​l​d​ ​s​e​t​T​e​x​t​:​@​"​"​]​;​
 ​ ​ ​ ​/​/​ ​D​i​s​m​i​s​s​ ​t​h​e​ ​k​e​y​b​o​a​r​d​
 ​ ​ ​ ​[​t​a​s​k​F​i​e​l​d​ ​r​e​s​i​g​n​F​i​r​s​t​R​e​s​p​o​n​d​e​r​]​;​
}​

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.

When a Cocoa Touch application quits or is sent to the background, it sends its delegate a message from the UIApplicationDelegate protocol so that the delegate can take care of business and respond to these events gracefully. In BNRAppDelegate.m, fill in the stubs of these two application delegate callbacks to save the to-do list:

-​ ​(​v​o​i​d​)​a​p​p​l​i​c​a​t​i​o​n​D​i​d​E​n​t​e​r​B​a​c​k​g​r​o​u​n​d​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​l​i​c​a​t​i​o​n​
{​
 ​ ​ ​ ​/​/​ ​T​h​i​s​ ​m​e​t​h​o​d​ ​i​s​ ​o​n​l​y​ ​c​a​l​l​e​d​ ​i​n​ ​i​O​S​ ​4​.​0​+​

 ​ ​ ​ ​/​/​ ​S​a​v​e​ ​o​u​r​ ​t​a​s​k​s​ ​a​r​r​a​y​ ​t​o​ ​d​i​s​k​
 ​ ​ ​ ​[​t​a​s​k​s​ ​w​r​i​t​e​T​o​F​i​l​e​:​d​o​c​P​a​t​h​(​)​ ​a​t​o​m​i​c​a​l​l​y​:​Y​E​S​]​;​
}​

-​ ​(​v​o​i​d​)​a​p​p​l​i​c​a​t​i​o​n​W​i​l​l​T​e​r​m​i​n​a​t​e​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​l​i​c​a​t​i​o​n​
{​
 ​ ​ ​ ​/​/​ ​T​h​i​s​ ​m​e​t​h​o​d​ ​i​s​ ​o​n​l​y​ ​c​a​l​l​e​d​ ​i​n​ ​i​O​S​ ​v​e​r​s​i​o​n​s​ ​p​r​i​o​r​ ​t​o​ ​4​.​0​

 ​ ​ ​ ​/​/​ ​S​a​v​e​ ​o​u​r​ ​t​a​s​k​s​ ​a​r​r​a​y​ ​t​o​ ​d​i​s​k​
 ​ ​ ​ ​[​t​a​s​k​s​ ​w​r​i​t​e​T​o​F​i​l​e​:​d​o​c​P​a​t​h​(​)​ ​a​t​o​m​i​c​a​l​l​y​:​Y​E​S​]​;​
}​

Now build and run your completed application. This exercise was intended to give you a taste of iOS development. There’s much, much more out there to do and learn.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset