Toll Free - 855-572-2777 / 855-572-APPS

Our Thinking

iPhone Programming Tutorial: Event Handling

By Ashish Khadse | In iPhone - iPad | on February 16, 2010

This blog is to familiarize you with the process of handling an event in an iPhone/iPod app.
We will create a demo project and walk you through the steps of handling an event, namely realizing a button click/touch on the iPhone/iPod screen.

Start Xcode and create a View based Application and name it “Event HandlingForBlog”.
To begin with, append EventHandlingForBlogViewController.h file

#import <UIKit/UIKit.h>
@interface EventHandlingForBlogViewController : UIViewController {
UIButton *btPrintM;
}
@property (nonatomic, retain) IBOutlet UIButton *btPrintM;

//This creates a property for your button declaring it as an outlet for use with view.

-(IBAction) printButtonPressed:(id) sender;
@end

printButtonPressed is the function to handle the event of user pressing the btPrintM button.

With this you’re done with the .h file.
Now, select the EventHandlingForBlogViewController.m file

#import "EventHandlingForBlogViewController.h"

@implementation EventHandlingForBlogViewController
@synthesize btPrintM;
- (void)viewDidUnload {
self.btPrintM = nil;
}
- (void)dealloc {
[btPrintM release];
[super dealloc];
}
-(IBAction) printButtonPressed : (id) sender{
NSLog(@"    Jai Ho");
}
@end

NSLog is like printf statement of C language. It prints onto the console.

Now, first save your project(Command+S).
Double-click on “EventHanldlingViewController.xib”. This will launch the Interface Builder.

In the Interface Builder ‘s Library, scroll down to “Round Rect Button” and drag it onto your View Window at a suitable position.
Double-click inside the button you dragged to edit its text, to say Print “Jai Ho”.

Now to link this button with that declared in the EventHanldlingForBlogViewController.h file, select the File Owner from Main Window and open its Connections Inspector(Command+2). Drag the “btPrintM” radio button from the connection inspector onto your button in View Window.

Linking Button


Now to link an event on your button to the function declared in EventHanldlingForBlogViewController.h file, select the button and press Command+2 or launch the Inspector Window (Shift+Command+I) and select Connections Inspector (the second tab). Now drag from the radio button named “Touch Up Inside” to the “File’s Owner” in the Main Window of the Interface Builder.
Touch Up Inside refers to the action of user touching the iPhone screen in the area highlighted by your button.
You can also select Touch Drag Inside/Outside, Touch Down, Touch Down Repeat, etc.

A drop-down will appear. Select “printButtonPressed” from it

Connecting to method


Connecting to method


And thats it, you’ve completed your project. You can close the Interface Builder now and return to Xcode.
Now, save your project(Command+S).
To run this project, click on the “Build & Go” button or (command + R).
The iPhone simulator will launch the application.

Output


Go ahead, click on the button. To check whether it printed or not, launch the console from Xcode. The console button is in the centre with “gdb” written over it, as shown below. Alternatively, you can select Run -> Console or press Shift+Command+R.

Console button


The console will display “Jai Ho”!!

Output on Console


You can download source code from here

No Comments to "iPhone Programming Tutorial: Event Handling"

    *

Leave a Reply

Your email address will not be published. Required fields are marked *


7 + = eleven

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Leave a Reply

Your email address will not be published. Required fields are marked *


× three = 15

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Copyright by Mobisoft Infotech LLC 2013, All rights reserved
GET IN TOUCH