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

Leave a Reply

Your email address will not be published. Required fields are marked *