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. Another Buttonpress reference you can find out from here ButtonView in iPhone

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 (See the below figure) . 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 Comment:

10 comments
Remco says June 21, 2011

Thanx for the tutorial. In step 7 I just couldn’t “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 ”

I’m using Xcode4 and the ‘Files Owner’ doesn’t allow a link to be made. Looked in the SourceCode you provided, but I couldn’t even find the buttons… Please help me.

Reply
    Sushant says June 23, 2011

    Checked the code once again, i have attached the screen shot. Hope this will help you.

    Reply
Stefan says June 22, 2011

Hi,

thanks a lot for your tutorial. I’ve downloaded your ButtonPress project and I wonder why the app crashes when I add a UITextField in a view and enter something.

Thanks in advance
Stefan

Reply
    Sushant says June 23, 2011

    I think something you did wrong when you added TextField. You just send us the error message.

    Reply
      Stefan says June 24, 2011

      Hi Sushant,

      I’ve figured it out. Everything works fine with Xcode 4.0, even with a UITextField. When I open the same product using Xcode 4.1 Developer Preview 6 and add a UITextField, the iPhone Simulator crashes after text input with an EXC_BAD_ACCESS.

      Seems to be a Xcode 4.1 related problem. Sorry for bothering you.

      Stefan

      Reply
        Sushant says June 24, 2011

        Check the Build setting, is every thing correct or not. Is this example running fine when you open in Xcode 4.1 ? or only after adding Text Field it shows exception.

        Reply
          Stefan says June 24, 2011

          The project is running fine in Xcode 4.0.2 and Xcode 4.1. The settings in both versions are the Xcode default settings. The project settings are untouched. I tried the following steps in both versions:

          – download ButtonPressed project
          – double click to open with Xcode
          – build & run

          in both versions everything works fine, so everything you’ve covered in this tutorial is working fine, even in Xcode 4.1.

          After adding an UITextField to a XIB:
          – build & run… success
          – switching to the XIB

          everything works fine. But when I enter just one letter, Xcode 4.1 crashes. This error does not occur using Xcode 4.0.

          I’m kind of desperate right now. I’ve followed your tutorial step by step and there’s nothing wrong… and I was happy. My first step alone and I’m stuck with this issue since a few days.

          Stefan

          Reply
Hezi says November 15, 2011

Hi
In step 7 I just couldn’t “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 ”

I’m using Xcode4 and the ‘Files Owner’ doesn’t allow a link to be made. Looked in the SourceCode you provided, but I couldn’t even find the buttons… Please help me.

Reply
napsters says December 31, 2011

Not working on Xcode4. Please Revise. Thanks

Reply
vasilis says April 28, 2012

Hi

Very good tutorial. I have followed your code to the letter but when I push the back button on any view (first, seconde or third) i get a EXC_BAD_ACCESS error. I am using xcode 4.2 and i dont have a MainWindow.xib. You think that this might be the problem?

Thank you

Reply
Add Your Reply