Saturday, July 12, 2014

Design Programming with Apple SDK - Part 4

Go Back - Design Programming with Apple SDK - Part 3

Making the objects work Together - Adding an Action Connection

Now we are going work on getting the button to work. The first step is to set the action for the button. With the storyboard open, we hide the Utilities view and open the Assistant editor by pressing the right view button and the center editor button at the top right of our window. The Assistant editor should have brought up the TestProjectViewController.h file to the right of the canvas screen. If you are seeing a different file, you can choose the file you want by clicking on the navigation icons below the Editor buttons.

With the button selected in our storyboard file we hold the control key while we drag over to the TestProjectViewController.h file. Drag to below the @interface line and you should see a dialog pop-up that says Insert Outlet, Action or Outlet Collection. Now you can release the mouse and a new popup will arise. For the Connection choose Action; for the name write in changeText: (with the colon); the Type should be id; the Event should be Touch Up Inside and the Arguments should be Sender. Then you can click Connect. A line of code should now have been added to the TestProjectViewController.h file that says:
- (IBAction)changeText:(id)sender;
Also an empty method has been created in TestProjectViewController.m as well that says:
- (IBAction)changeText:(id)sender {
}
In a nutshell, these two files work together. The .h file is the header file that contains the variable and method declarations, while the .m file contains the main body of the code.

At this point we have created an action connection between the Button and the View Controller. The next step is to create an outlet connection between our Label and the View Controller. This is a similar process.

Read Next - Design Programming with Apple SDK - Part 5

No comments: