How To Change Background Color After Pressing Button In iPhone

  • Home >>
  • iOS
  • >> How To Change Background Color After Pressing Button In iPhone

In this application we will see how to change background color using button pressed. So let see how it will worked.  My previous post you can find out from here  ButtonPressed

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

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: Open the BackgroungColorViewController.h file and make the following changes in the file

#import <uikit/UIKit.h>

@interface BackgroungColorViewController : UIViewController {

IBOutlet UIButton*firstButton;
IBOutlet UIButton*secondButton;
IBOutlet UIButton*thirdButton;
}

-(IBAction)FirstButtonPressed:(id)sender;
-(IBAction)SecondButtonPressed:(id)sender;
-(IBAction)ThirdButtonPressed:(id)sender;

@end

Step 4: Double click the BackgroungColorViewController.xib file and open it to the Interface Builder. First drag the three Round Rect Button and place it to the view window. Select the first button and bring up Connection Inspector and connect Touch Up Inside to the Files Owner icon and select FirstButtonPressed: method. Do the same thing for other two buttons and select SecondButtonPressed: and ThirdButtonPressed: method. Now save the .xib file, close it and go back to the Xcode.

Step 5: In the BackgroungColorViewController.m file make the following changes:


#import "BackgroungColorViewController.h"
@implementation BackgroungColorViewController

-(IBAction)FirstButtonPressed:(id)sender
{
self.view.backgroundColor = [UIColor greenColor];
}

-(IBAction)SecondButtonPressed:(id)sender
{
self.view.backgroundColor = [UIColor yellowColor];
}

-(IBAction)ThirdButtonPressed:(id)sender
{
self.view.backgroundColor = [UIColor redColor];
}

- (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];
// 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 6: Now compile and run the application on the simulator.

You can Download SourceCode from here BackgroungColor

About the Author

Leave a Reply 2 comments

pavan kumar r.m - July 3, 2011 Reply

it is very easy to the iphone developer to grasp the coding and analyze through ur website i had got many sample coding through ur website and it made me easier to develop the code for my projects,so please post as many as codings
that make a developer or learner easier.

PAVAN KUMAR R.M.

Lance - November 26, 2011 Reply

Hi! I was doing well and have all the codes in this example. The problem is I cannot connect the buttons. When I try to connect the buttons according to these instructions:
“and select FirstButtonPressed: method. Do the same thing for other two buttons and select SecondButtonPressed: and ThirdButtonPressed: method. Now save the .xib file, close it and go back to the Xcode.”
There is nothing to connect inside File’s Owner Icon.
How can I fix this?

Leave a Reply: