Wiring up the button

A button works using target-action, like you learned about in Chapter 27. The button’s action is the message that you want sent when the button is tapped. The button’s target is the object to which that message should be sent.

In BNRAppDelegate.m, give the Insert button a target-action pair:

#​p​r​a​g​m​a​ ​m​a​r​k​ ​-​ ​A​p​p​l​i​c​a​t​i​o​n​ ​d​e​l​e​g​a​t​e​ ​c​a​l​l​b​a​c​k​s​

-​ ​(​B​O​O​L​)​a​p​p​l​i​c​a​t​i​o​n​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​l​i​c​a​t​i​o​n​
d​i​d​F​i​n​i​s​h​L​a​u​n​c​h​i​n​g​W​i​t​h​O​p​t​i​o​n​s​:​(​N​S​D​i​c​t​i​o​n​a​r​y​ ​*​)​l​a​u​n​c​h​O​p​t​i​o​n​s​
{​
 ​ ​ ​ ​.​.​.​

 ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​a​n​d​ ​c​o​n​f​i​g​u​r​e​ ​a​ ​r​o​u​n​d​e​d​ ​r​e​c​t​ ​I​n​s​e​r​t​ ​b​u​t​t​o​n​
 ​ ​ ​ ​s​e​l​f​.​i​n​s​e​r​t​B​u​t​t​o​n​ ​=​ ​[​U​I​B​u​t​t​o​n​ ​b​u​t​t​o​n​W​i​t​h​T​y​p​e​:​U​I​B​u​t​t​o​n​T​y​p​e​R​o​u​n​d​e​d​R​e​c​t​]​;​
 ​ ​ ​ ​s​e​l​f​.​i​n​s​e​r​t​B​u​t​t​o​n​.​f​r​a​m​e​ ​=​ ​b​u​t​t​o​n​F​r​a​m​e​;​

 ​ ​ ​ ​/​/​ ​G​i​v​e​ ​t​h​e​ ​b​u​t​t​o​n​ ​a​ ​t​i​t​l​e​
 ​ ​ ​ ​[​s​e​l​f​.​i​n​s​e​r​t​B​u​t​t​o​n​ ​s​e​t​T​i​t​l​e​:​@​"​I​n​s​e​r​t​"​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​f​o​r​S​t​a​t​e​:​U​I​C​o​n​t​r​o​l​S​t​a​t​e​N​o​r​m​a​l​]​;​

 ​ ​ ​ ​/​/​ ​S​e​t​ ​t​h​e​ ​t​a​r​g​e​t​ ​a​n​d​ ​a​c​t​i​o​n​ ​f​o​r​ ​t​h​e​ ​I​n​s​e​r​t​ ​b​u​t​t​o​n​
 ​ ​ ​ ​[​s​e​l​f​.​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​]​;​

 ​ ​ ​ ​.​.​.​

 ​ ​ ​ ​r​e​t​u​r​n​ ​Y​E​S​;​
}​

The target is self, and the action is addTask:. Thus, when the Insert button is tapped, it will send the addTask: message to the BNRAppDelegate. The next step, then, is to implement the addTask: method in BNRAppDelegate.

Eventually, addTask: will retrieve the text entered in taskField and add it to the tasks array. The task will then appear in the table view. But because you have not yet wired up the table view, you are going to implement addTask: to retrieve the text from taskField and simply log it to the console.

In BNRAppDelegate.m, add an implementation of addTask: at the bottom of the file along with a new pragma mark:

@​i​m​p​l​e​m​e​n​t​a​t​i​o​n​ ​B​N​R​A​p​p​D​e​l​e​g​a​t​e​

#​p​r​a​g​m​a​ ​m​a​r​k​ ​-​ ​A​p​p​l​i​c​a​t​i​o​n​ ​d​e​l​e​g​a​t​e​ ​c​a​l​l​b​a​c​k​s​

.​.​.​

#​p​r​a​g​m​a​ ​m​a​r​k​ ​-​ ​A​c​t​i​o​n​s-​ ​(​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​a​s​k​
 ​ ​ ​ ​N​S​S​t​r​i​n​g​ ​*​t​e​x​t​ ​=​ ​[​s​e​l​f​.​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​e​x​t​ ​l​e​n​g​t​h​]​ ​=​=​ ​0​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​r​e​t​u​r​n​;​
 ​ ​ ​ ​}​

 ​ ​ ​ ​/​/​ ​L​o​g​ ​t​e​x​t​ ​t​o​ ​c​o​n​s​o​l​e​
 ​ ​ ​ ​N​S​L​o​g​(​@​"​T​a​s​k​ ​e​n​t​e​r​e​d​:​ ​%​@​"​,​ ​t​e​x​t​)​;​

 ​ ​ ​ ​/​/​ ​C​l​e​a​r​ ​o​u​t​ ​t​h​e​ ​t​e​x​t​ ​f​i​e​l​d​
 ​ ​ ​ ​[​s​e​l​f​.​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​
 ​ ​ ​ ​[​s​e​l​f​.​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​]​;}​

@​e​n​d​

What is this resignFirstResponder business? Here is the short version:

Some view objects are also controls. A control is a view that the user can interact with. Buttons, sliders, and text fields are examples of controls. (Keep in mind that the term control has nothing to do with controllers in MVC.)

When there are controls on the screen, one of them can be the first responder. When a control has first responder status, it gets the first chance to handle text input from the keyboard and shake events (such as when the user shakes the device to undo the last action).

When the user interacts with a control that can accept first responder status, that control is sent the becomeFirstResponder message. When a control that accepts text input (like a text field) becomes the first responder, the keyboard appears on the screen. At the end of addTask:, you tell the text field to resign its status, which causes the keyboard to disappear.

Build and run the application. (You will need to stop the currently running instance of iTahDoodle before Xcode can build and run again.)

Once iTahDoodle is running again, enter something in the text field, click Insert, and confirm that the text is logged to the console.

You may also see a warning in the console that reads Application windows are expected to have a root view controller at the end of application launch. You can ignore this. To stick with the absolute basics, you are not implementing a root view controller for iTahDoodle, and this simple application will work fine without one. You will learn all about view controllers in iOS Programming: The Big Nerd Ranch Guide or any other book on iOS development.

Here is an updated object diagram showing the target-action pair.

Figure 31.11  Updated iTahDoodle object diagram

Updated iTahDoodle object diagram
..................Content has been hidden....................

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