iOS: Working with Actions & Outlets

Action | Cocoa Touch | IOS | IPhone | Objective-C

iPhone Application Development


This entry is part 7 of 9 in the series iPhone Application Development

Objective-C Actions are just like event in java or dotnet programing, if you are familiar with one of them. like when user performs certain acivity on UI and respectively we have to perform some action in our application like click on a button. We just saw how outlets work. Now let’s create another application that uses actions too.

Getting Started with Objective-C Actions

Step 1. Create new iPhone application with single view application template and name it ActionDemo. As we expected we will get 2 controller files, 2 delegate files and 1 user interface file. Open user interface file and drag 2 labels, 1 text field and 1 button and design view like below:

Objective-C Actions

Let’s create 2 outlets first, One for Text field and another for label, which is below GO button. Click on second label and drag while holding control key to interface block of your view controller’s interface file (ActionDemoViewController.h). you will get popup like below one.

objective-c actions

Set outputLabel in Name field and click on connect. Create another outlet and set connection property like below.

objective-c actions

Step 2. Next step is to create a action for our button. This can be done same way as we define outlet. CTRL+drag button from user interface to View Controller and you will get similar popup.

objective-c actions

First set Connection to Action instead of outlet. Then set type to UIButton instead of id. Name it goClicked and hit connect. Now delete { } code auto written in @interface block of view controller’s header file (.h file). So your view controller’s header file will look like mine. Also delete extra methods from implementation file.

objective-c actions


Step 3. Update implementation file to specify @synthesize to store data in which variable.

Step 4. Also check that your implementation file contains a method called goClicked which is created when we declare an action for our go button. Lets put some code in that method. We want to set welcome message in output label like: Welcome Username.

First we create an NSString object called username and store textfield’s text, which we can access with the outlet. That’s what outlet do. Next we create another NSString and concat username with Welcome message and create a single string message. Then we access output label via outlet and set welcome message.

We can do same thing by updating above code as this:

With this single line of code we can perform same operation. That’s the power of Objective-C language. Now click on run. Type your name and click on go and see output. And it’s that easy to create objective-c actions. See iPhone development is not that scary.

objective-c actions
Series Navigation<< iPhone SDK: MVC in iPhone Application Development<< iPhone SDK: Your First iPhone Application

Bookmark Link using: bookmark at folkd