ButtonPressed Example In iPhone.

This is the ButtonPressed example. This is the very simple example. In this example we will see after pressing button message will display and dragging in the screen message will hide. So let see how it will worked. Another ButtonPress example you can find out from here ButtonPressed

Step 1: Open the Xcode, Create a new project using View base application. Give the application “ButtonFun_View”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: Xpand classes and notice Interface Builder created the ButtonPressViewController class for you and generated a separate nib, ButtonFun_ViewViewController.xib for the “ButtonFun_View”.

Step 4: Open the ButtonFun_ViewViewController.h file and make the following chanages:


#import <uikit/UIKit.h>

@interface ButtonFun_ViewViewController : UIViewController {

IBOutlet UIButton *button;
IBOutlet UILabel *label;

@property (nonatomic, retain) UIButton *button;
@property (nonatomic, retain) UILabel *label;
-(IBAction) buttonPressed;




Step 5: Double click the ButtonFun_ViewViewController.xib file and open it to the Interface Builder. First drag the Round Rect button from the library and place it to the view window, next drag the label from the library and place it to the view window. Select the Round Rect button and bring up Connection Inspector and connect Touch up Inside to the Files Owner icon and select buttonPressed: method. and connect Files Owner icon to the label and select label. Now save the .xib file, save it and go back to the Xcode.

Step 6: In the ButtonFun_ViewViewController.m file make the following changes:


#import "ButtonFun_ViewViewController.h"

@implementation ButtonFun_ViewViewController

@synthesize button;
@synthesize label;

-(IBAction) buttonPressed {
label.text = @"Drag to hide label.";

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

button.hidden = YES;
label.hidden = YES;

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
button.hidden = NO;
label.hidden = NO;
label.text = @"";

- (void)dealloc
[super dealloc];

- (void)didReceiveMemoryWarning
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.

#pragma mark - View lifecycle

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
[super viewDidLoad];

- (void)viewDidUnload
[super viewDidUnload];


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
return (interfaceOrientation == UIInterfaceOrientationPortrait);




Step 7: Now compile and run the application on the Simulator.

You can Download SourceCode from here ButtonFun_View

Leave a Comment:

sunaina says June 27, 2011

hey we tried ur code in example bt ist nt wrking 🙁

    Sushant says June 28, 2011

    Have you got any error message? Because it working perfectly. After pressing button it will show the message and drag any where in the screen the message will be hide. Let us know if you got any error message.


sunaina says July 1, 2011

hey buddy its wrking nw prob ws der with xcode 😛 thanks

Add Your Reply