- iPhone SDK: Objective-C Condition
- iPhone SDK Components & Requirements
- Overview of iOS Platform
- Objective-C Tutorial with xCode
- iPhone SDK: Objective-C Creating Custom Class
- iPhone SDK: Using Existing Objective-C Classes
- iPhone SDK: Your First iPhone Application
- iPhone SDK: MVC in iPhone Application Development
- iOS: Working with Actions & Outlets
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:
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.
Set outputLabel in Name field and click on connect. Create another outlet and set connection property like below.
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.
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.
Step 3. Update implementation file to specify @synthesize to store data in which variable.
1 2 3 |
@implementation ActionDemoViewController @synthesize outputLabel = _outputLabel; @synthesize username = _username; |
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.
1 2 3 4 5 6 7 |
- (IBAction)goClicked:(id)sender { NSString *username = self.username.text; NSString *msg = [NSString stringWithFormat:@"Welcome %@",username]; self.outputLabel.text = msg; } |
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:
1 2 3 4 5 6 |
- (IBAction)goClicked:(id)sender { self.outputLabel.text = [NSString stringWithFormat: @"Welcome %@",self.username.text]; } |
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.