In this chapter, we're re going to write a slightly more complex application-one that will feature two buttons as well as a label
In this chapter, we need to write a slightly more complex application.ProgramUsed to control two buttons and labels.
Creating our project
Launch xcode and select File into new versions new project. Select the single view application template, and then click Next.
In the product name field, type the name of our new application, button fun. the company Identifier Field shocould still have the value you used in the previous chapter, so you can leave that alone. in the class prefix field, use the same value as you did in the previous chapter: bid.
Select iPhone for device family. we're not going to use storyboar DS or unit tests, so you can leave both of those options unchecked. howeve R, we do want to use arc, so check the use automatic reference counting box. we'll explain arc la ter in the chapter.
Looking at the View Controller
The button fun folder shoshould contain four source code files (the ones that end in. h or. m) and a single NIB file. these four source code files implement two classes that our application needs: Our application delegate and the View Controller for our application's only view. notice that xcode automatically added the prefix we specified to all of our class names.
The "button fun" folder should contain four source files (two H files and two m Files). The four files implement two classes required by our application: app delegate and View Controller.
Understanding outlets and actions
A controller class can refer to objects in a NIB file by using a special kind of property calledOutlet.
The Controller class can be associated with objects through the NIB file, because an object calledOutlet.
Interface objects in our NIB file can be set up to trigger special methods in our controller class. These special methods are knownActionMethods (or justActions).
The interface object in the NIB file can be set to start the specified method. The specified method isAction
Outlets
Outlets are special objective-C Class properties that are declared using the keywordIboutlet. Declaring an outlet is done in your controller Class header file, and might look something like this:
@ Property (nonatomic, retain) iboutlet uibutton * mybutton;
This example is an outlet called mybutton, which can be set to point to any button in interface builder. The iboutlet keyword is defined like this:
# Ifndef iboutlet # define iboutlet # endif
Outlet changes
In the first version of this book, we declared both a property and its underlying instance variable for our outlets. at that time, properties were a new construct in the objective-C language, and they required you to declare a corresponding instance variable, like this:
@ Interface myviewcontroller: uiviewcontroller {uibutton * mybutton;} @ property (nonatomic, retain) uibutton * mybutton; @ end
In this version, Apple has removed the iboutlet keyword from the instance variable. As follows:
@ Property (nonatomic, retain) iboutlet uibutton * mybutton;
Actions
In a nutshell, actions are methods that ar e declared with a special return type, ibaction, which tells interface builder that this method can be triggered by a control in a NIB file.
The Declaration for an act ion method will usually look like this:
-(Ibaction) dosomething :( ID) sender; or like this:-(ibaction) dosomething;
Cleaning up the View Controller
Delete all the methods before t for viewdidunload. When you're finished, your implementation shoshould look like this:
# Import "bidviewcontroller. H "@ implementation bidviewcontroller-(void) viewdidunload {[Super viewdidunload]; // release any retained subviews of the main view. // e.g. self. myoutlet = nil;} @ end
Designing the user interface