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;



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

self.view.backgroundColor = [UIColor greenColor];

self.view.backgroundColor = [UIColor yellowColor];

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);


Step 6: Now compile and run the application on the simulator.

You can Download SourceCode from here BackgroungColor

About the Author

Leave a Reply 1 comment

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.


Leave a Reply: