ButtonPressed example in iPhone

This is the ButtonPressed example. In this example we will see how to load another view after pressing button. So let see how it will worked.

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

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, ButtonPressViewController.xib for the “ButtonPress”.

Step 4: We need to add three UIViewController in the class in the project. So select the project -> New File -> Cocoa Touch ->ViewController class and give the class name ”FirstView”, “SecondView” and “ThirdView”.

Step 5: We need to add background image in the Resource folder.

Step 6: Open the ButtonPressViewController.h file and make the following changes in the file:

#import <UIKit/UIKit.h>

@class ButtonPressViewController;
@interface ButtonPressAppDelegate : NSObject  {
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet ButtonPressViewController *viewController;

@end

Step 7: Double click the ButtonPressViewController.xib file and open it to the Interface Builder. First select view and bring up Attributes Inspector change the background color “Black”. Now drag three button from the library and place it to the view window. Select the buttons from view window and bring up Attribute Inspector and change the Title name into “FirstView”,”SecondView” and “ThirdView”. Select the FirstView button from view and bring up Connection Inspector and connect Touch Up Inside to the Files owner icon and select FirstView: method . Do the same thing for other two buttons and select SecondView: and ThirdView: method. Save the .xib file, close it and go back to the Xcode.

Step 8: In the  ButtonPressViewController.m file make the following changes in the file:

#import "ButtonPressViewController.h"
#import "FirstView.h"
#import "SecondView.h"
#import "ThirdView.h"

@implementation ButtonPressViewController

(IBAction)FirstView:(id)sender
{
FirstView*firstView = [[FirstView alloc]
initWithNibName:@"FirstView"
bundle:nil];
[self.view addSubview:firstView.view];
}

(IBAction)SecondView:(id)sender
{
SecondView*secondView = [[SecondView alloc]
initWithNibName:@"SecondView"
bundle:nil];
[self.view addSubview:secondView.view];
}

(IBAction)ThirdView:(id)sender
{
ThirdView*thirdView = [[ThirdView alloc]
initWithNibName:@"ThirdView"
bundle:nil];
[self.view addSubview:thirdView.view];
}

(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

(void)viewDidUnload
{
[super viewDidUnload];

}

(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

Step 9: Open the FirstView.h file and make the following changes in the file:

#import <UIKit/UIKit.h>

@interface FirstView : UIViewController {

}
(IBAction)BackButton:(id)sender;

@end

Step 10: Double click the FirstView.xib file and open it to the Interface Builder. First select the view and bring up Attribute Inspector and change the background color. Drag the label from the library and place it to the view window and change the Text of the label. Drag the Navigation Bar from the library and place it to the top position of the view window. Now drag the button from the library and place it on the navigation bar. Select the button and bring up Attribute Inspector and select the “backbutton.png” and bring up Connection Inspector, connect Touch Up Inside to the File’s Owner icon and select BackButton: method. Now save the .xib file , close it and go back to Xcode.

Step 11: In the FirstView.m file make the following changes in the file:

#import "FirstView.h"

@implementation FirstView

(IBAction)BackButton:(id)sender
{
[self.view removeFromSuperview];
}

(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

(void)dealloc
{
[super dealloc];
}

(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];

}

#pragma mark – View lifecycle

(void)viewDidLoad
{
[super viewDidLoad];

}

(void)viewDidUnload
{
[super viewDidUnload];

}

(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

Step 12: Open the SecondView.h file and make the following changes in the file:

#import <UIKit/UIKit.h>

@interface SecondView : UIViewController {

}

(IBAction)BackButton:(id)sender;

@end

Step 13: Double click the SecondView.xib file and open it to the Interface Builder. First select the view and bring up Attribute Inspector and change the background color. Drag the label from the library and place it to the view window and change the Text of the label. Drag the Navigation Bar from the library and place it to the top position of the view window. Now drag the button from the library and place it on the navigation bar. Select the button and bring up Attribute Inspector and select the “backbutton.png” and bring up Connection Inspector, connect Touch Up Inside to the File’s Owner icon and select BackButton: method. Now save the .xib file , close it and go back to Xcode.

Step 14: In the SecondView.m file make the following changes:

#import "SecondView.h"

@implementation SecondView

(IBAction)BackButton:(id)sender
{
[self.view removeFromSuperview];
}

(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

(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

(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}

(void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

Step 15: Open the ThirdView.h file and make the following changes :

#import <UIKit/UIKit.h>

@interface ThirdView : UIViewController {

}
(IBAction)BackButton:(id)sender;
@end

Step 16: Double click the ThirdView.xib file and open it to the Interface Builder. First select the view and bring up Attribute Inspector and change the background color. Drag the label from the library and place it to the view window and change the Text of the label. Drag the Navigation Bar from the library and place it to the top position of the view window. Now drag the button from the library and place it on the navigation bar. Select the button and bring up Attribute Inspector and select the “backbutton.png” and bring up Connection Inspector, connect Touch Up Inside to the File’s Owner icon and select BackButton: method. Now save the .xib file , close it and go back to Xcode.

Step 17: In the ThirdView.m file make the following changes:

#import "ThirdView.h"

@implementation ThirdView

(IBAction)BackButton:(id)sender
{
[self.view removeFromSuperview];
}

(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

(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

(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}

(void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

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

You can Download SourceCode from here ButtonPress

Leave a Reply

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