In this tutorial we will see how make a outlet and action in iphone. Before we are going to this you need to understand in iPhone programming is outlets and actions. If you are familiar with traditional programming languages such as Java or C#, this is a concept that requires some time to get used to the concepts are similar, just that it is a different way of doing things.
1. Using Xcode, create a View-based Application (iPhone) project and name it OutletsAndActions.
2. Edit the OutletsAndActionsViewController.xib file by double-clicking it to open it in Interface Builder. When Interface Builder is loaded, double-click the View item in the OutletsAndActionsViewController.xib window to visually display the View window. Populate the three views onto the View window — Label, Text Field, and Round Rect Button. Set the Label view with the text “Enter your name” by double-clicking on it. Also, set the Round Rect Button with the “OK” string.
3. In Xcode, modify the OutletsAndActionsViewController.h file with the following statements:
#import <UIKit/UIKit.h>
@interface OutletsAndActionsViewController : UIViewController
{
//---declaring the outlet---
IBOutlet UITextField *txtName;
}
//---expose the outlet as a property---
@property (nonatomic, retain) UITextField *txtName;
//---declaring the action---
-(IBAction) btnClicked:(id) sender;
@end;
4. In the OutletsAndActionsViewController.m file, define the following statements:
#import “OutletsAndActionsViewController.h”
@implementation OutletsAndActionsViewController
//---synthesize the property---
@synthesize txtName;
//---displays an alert view when the button is clicked---
-(IBAction) btnClicked:(id) sender {
NSString *str = [[NSString alloc]
initWithFormat:@”Hello, %@”, txtName.text];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@”Hello!”
message:str
delegate:self
cancelButtonTitle:@”Done”
otherButtonTitles:nil];
[alert show];
[str release];
[alert release];
}
- (void)dealloc {
//---release the outlet---
[txtName release];
[super dealloc];
}
5. In the OutletsAndActionsViewController.xib window, control-click and drag the File’s Owner item to the Text Field view. A popup will appear; select the outlet named txtName.
6. Control-click and drag the OK Button view to the File’s Owner item. Select the action named btnClicked:.
7. Right-click the OK Button view to display its events. Notice that the Button view has several events, but one particular event — Touch Up Inside — is now connected to the action you specified (btnClicked:). Because the Touch Up Inside event is so commonly used, it is automatically connected to the action when you control-click and drag it to the File’s Owner item. To connect other events to the action, simply click the circle displayed next to each event and then drag it to the File’s Owner item.


Categories:



