Home Tutorial Iphone Examples iphone Button Action (changing background of window)

 
 

Share on Google+Share on Google+
iphone Button Action (changing background of window)
Posted on: September 1, 2009 at 12:00 AM
Advertisement
In this tutorial we will change the iPhone background color on clicking the button, This is Window Based application.

iphone Button Action (changing background color of window)

 

In this tutorial we will change the background color on clicking the button, This is Window Based application.

Final application will look like this:

In .h file add five variables (red, green, blue, yellow, orange). Each is of type UIButton *, pronounced as a "UIButton pointer" and @property is declared.  

  UIButton *Orange;

    UIButton *Green;

    UIButton *Red;

    UIButton *Blue;

    UIButton *yellow;

@property (nonatomic, retain) IBOutlet UIButton *Orange;

@property (nonatomic, retain) IBOutlet UIButton *Green;

@property (nonatomic, retain) IBOutlet    UIButton *yellow;

@property (nonatomic, retain) IBOutlet    UIButton *Blue;

@property (nonatomic, retain) IBOutlet    UIButton *Red;  

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

 Action is also declared into .h file

-(IBAction)doOrange;

-(IBAction)doBlue;

-(IBAction)doRed;

- (IBAction)doGreen;

- (IBAction)doyellow;  

 

 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 "Orange" button is pressed, it will call the doOrange function.

  In .m file will synthesize the variables:  

 @synthesize  Orange, Green, yellow, Blue, Red ;

    -(IBAction)doOrange

         {

      window.backgroundColor = [UIColor orangeColor];

        }

   -(IBAction)doBlue

            {

         window.backgroundColor = [UIColor blueColor];

               }

            -(IBAction)doRed

                        {

                    window.backgroundColor = [UIColor redColor];

                     }

    

               - (IBAction)doGreen

               {

                window.backgroundColor = [UIColor greenColor];

                }

   - (IBAction)doyellow

                   {

          window.backgroundColor = [UIColor yellowColor];

                     }

The @synthesize keyword tells the compiler to generate getter/setter functions for the named variables. For example, the compiler generates a "orangeColor" as the getter function, and a "setOrangeColor" as the setter function.

The IBAction methods set the background color of the window to the selected color. You could do anything in these functions; for example, play a different sound, or display a different image.

  Connect all the variables in interface builder and also all action of button.

   

 .m file will look like:  

 #import "BackgroundColorAppDelegate.h"

@implementation BackgroundColorAppDelegate

@synthesize window, Orange, Green, yellow, Blue, Red ;

 -(IBAction)doOrange

{
window.backgroundColor = [UIColor orangeColor];

}

 -(IBAction)doBlue

{

    window.backgroundColor = [UIColor blueColor];

}

 -(IBAction)doRed

{

    window.backgroundColor = [UIColor redColor];

}

 - (IBAction)doGreen

{

    window.backgroundColor = [UIColor greenColor];

}

 - (IBAction)doyellow

{
  window.backgroundColor = [UIColor yellowColor];
}

 - (void)applicationDidFinishLaunching:(UIApplication *)application

 {   

    [window makeKeyAndVisible];

}

 - (void)dealloc

{

    [window release];

    [super dealloc];

}  

@end  

Click to Download Code

Advertisement

Related Tags for iphone Button Action (changing background of window) :


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.

Posted on: September 1, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com