Share on Google+Share on Google+

iphone button change view

In this tutorial will learn button change view i.e. when button is clicked will move to next view.

iphone button change view

Final application will look like this:


In this tutorial will learn button change view i.e. when button is clicked will move to next view. This application is View Based Application in that will add another View Controller into button_imageViewController.xib and will assign that View Controller on button so whenever button is clicked will move to View Controller and on that will add view and in that view will add Button and datepicker. Datepicker is used to show the date and Back button is used to move to previous view, to add ViewController on button will make variable for that View Controller and will call that ViewController by its variable.

my project name is button_image, view based application.

Make changes in button_imageViewController.h and .m file.

In .h file add this:

IBOutlet UITextField *txt;

     IBOutlet UIViewController *pick;

     IBOutlet UIDatePicker *mdatepicker;

     IBOutlet UIButton *back;

      @property (nonatomic, retain) IBOutlet UITextField *txt;

      @property (nonatomic, retain) IBOutlet UIViewController *pick;

      @property (nonatomic, retain) IBOutlet UIDatePicker *mdatepicker;

        -(IBAction) nextview:(id)sender;

        -(IBAction) previousview:(id)sender;  

The IBOutlet keyword is used to notify Interface Builder that these values will connect to actual UIButtons that are laid out with Interface Builder.

The IBAction keyword is used to notify Interface Builder that these methods will be called by controls laid out by Interface Builder. For example, when the "Custom" button is pressed, it will call the nextview function.

After this open Interface Builder for button_imageViewController.xib, in that add ViewController. ViewController is added because we have to display datepicker on it and also the Back button to move to previous view and on that ViewController add View so that we can place our Datepicker on it as well as Rounded button, without adding view on the ViewController you cannot add anything.

NOTE:- In first view place button on TextField and button should be custom and set the image on it. Button width must be more when compared to TextField.

The @synthesize keyword tells the compiler to generate getter/setter functions for the named variables. The IBAction methods set the view. You could do anything in these functions, for example, play a different sound, or display a different image.

In .m file add this:

@synthesize txt, pick, mdatepicker;

  // this is to move to next view

-(IBAction) nextview:(id)sender


    [self.view addSubview:pick.view];


  // to hide keyboard

-(void) touchesBegan :(NSSet *) touches withEvent:(UIEvent *)event


    [txt resignFirstResponder];

    [super touchesBegan:touches withEvent:event ];


  // this is to show previous view on clicking back button

-(IBAction) previousview:(id)sender


    [pick.view removeFromSuperview];

 Make connection as shown below

 Finally press Build And Go s

Download the Source Code






Posted on: September 2, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null