Thursday, January 10, 2013

A simple color changer app in ios

The task is that i have a paragraph in a text View controller and i have a segmented control with number of colors can be selected.  Once I select a color, my text should change accordingly. look for the output below.


It's quit simple, isn't it. ok let's check the code. keep in mind even if it is a simple app, you have to deal with three files. one is the design view with the .XIB extension. other two are your header file (with .h extension) and the implementation file (with .m extension). 

the design is simple. you have to drag and drop a segmented control and assign selected segment indexes from 0 to 3. Also have a Text view controller with some sample text in it.

here's the code in the header file.





//  ColorChangerViewController.h

#import <UIKit/UIKit.h>

@interface ColorChangerViewController : UIViewController {
    
 //need two IBOutlets for the TextView Controller and the segmented controller
    IBOutlet UISegmentedControl *colourChanger;
    IBOutlet UITextView *txtTextContainer;
        
}

@property(nonatomic,retain) UISegmentedControl  *colourChanger;
@property (nonatomic,retain) UITextView *txtTextContainer;

-(IBAction) changeColor;       //we have one function call changeColor() to work with the button click

@end


Code for the implementation file is as below. note that there is a number of automatically generated lines of code. i have highlited only the parts that i have implemented 


//  ColorChangerViewController.m

#import "ColorChangerViewController.h"

@implementation ColorChangerViewController

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

@synthesize colourChanger;
@synthesize txtTextContainer;

-(IBAction) changeColor
{
    if(colourChanger.selectedSegmentIndex==0)
    {
        txtTextContainer.textColor=[UIColor blueColor];
    }
    if(colourChanger.selectedSegmentIndex==1)
    {
        txtTextContainer.textColor=[UIColor greenColor];
    }
    if(colourChanger.selectedSegmentIndex==2)
    {
        txtTextContainer.textColor=[UIColor redColor];
    }
    if(colourChanger.selectedSegmentIndex==3)
    {
        txtTextContainer.textColor=[UIColor orangeColor];
    }

}

@end

Now you can build and run your app. I also have made available the project source code for you. 




No comments:

Post a Comment