Here is How You Use the Segmented Control [UISegmentedControl]

Elevator Switch

by mattjdrake on September 15, 2009

A segmented control displays a list of options that a user can choose from. Each segment sort of looks like a button; the segments remains “pressed” even after the user lifts his or her finger.

You can detect when a different segment is selected and also what corresponding value in an array (that you supply) is referenced by the selected segment. Here is an example of what a UISegmentedControl looks like:

Picture-Of-A-Segmented-Control

Today I can going to show you how to use a segmented control in Objective-C code using the UISegmentedControl class. First thing is a video followed by the written instructions:

Here Are the Steps To Use UISegmentedControl

  • Create A View Based XCode Project
  • Add A UILabel To the Controller:
  • 	#import "UseSegmentedControlViewController.h"
    
    	@implementation UseSegmentedControlViewController
    
    	UILabel *label;
    
    	- (void)viewDidLoad {
    	    [super viewDidLoad];
    
    		//Create label
    		label = [[UILabel alloc] init];
    		label.frame = CGRectMake(10, 10, 300, 40);
    		label.textAlignment = UITextAlignmentCenter;
    		[self.view addSubview:label]; 
    
    	}
    
    	- (void)dealloc {
    		[label release];
    	    [super dealloc];
    	}
    
    	@end
  • Create An Array:
  • NSArray *itemArray = [NSArray arrayWithObjects: @"One", @"Two", @"Three", nil];
    
  • Create An Instance of UISegmentedControl:
  • UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
    
  • Customize the Control By Setting It’s Properties:
  • segmentedControl.frame = CGRectMake(35, 200, 250, 50);
    segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
    segmentedControl.selectedSegmentIndex = 1;
    
  • Set A Target and Action (The Code That Will Execute When The User Selects A New Segment):
  • [segmentedControl addTarget:self
                         action:@selector(pickOne:)
               forControlEvents:UIControlEventValueChanged];
    
  • Add the Segmented Control to the view:
  • [self.view addSubview:segmentedControl];
    [segmentedControl release];
    
  • Implement the Action:
  • - (void) pickOne:(id)sender{
    	UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
    	label.text = [segmentedControl titleForSegmentAtIndex: [segmentedControl selectedSegmentIndex]];
    }
    
  • Release the UILabel in dealloc:
  • - (void)dealloc {
    	[label release];
    	[super dealloc];
    }
    

    Here Is the Complete Code For the UIViewController:

    //  Segmented Control code originally from Code_Toolbox Bonus Item
    //    from the ebook @
    //      http://howtomakeaniphoneapp.com
    //
    #import "UseSegmentedControlViewController.h"
    
    @implementation UseSegmentedControlViewController
    
    UILabel *label;
    
    - (void)viewDidLoad {
    	[super viewDidLoad];
    
    	//Create label
    	label = [[UILabel alloc] init];
    	label.frame = CGRectMake(10, 10, 300, 40);
    	label.textAlignment = UITextAlignmentCenter;
    	[self.view addSubview:label]; 
    
    	//Create the segmented control
    	NSArray *itemArray = [NSArray arrayWithObjects: @"One", @"Two", @"Three", nil];
    	UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
    	segmentedControl.frame = CGRectMake(35, 200, 250, 50);
    	segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
    	segmentedControl.selectedSegmentIndex = 1;
    	[segmentedControl addTarget:self
    	                     action:@selector(pickOne:)
    	           forControlEvents:UIControlEventValueChanged];
    	[self.view addSubview:segmentedControl];
    	[segmentedControl release];
    
    }
    
    //Action method executes when user touches the button
    - (void) pickOne:(id)sender{
    	UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
    	label.text = [segmentedControl titleForSegmentAtIndex: [segmentedControl selectedSegmentIndex]];
    } 
    
    - (void)dealloc {
    	[label release];
    	[super dealloc];
    	}
    
    @end

    Be Sure To Check Out the Header Files In XCode To See All Your Options

    You can change the look and feel of the UISegmentedControl by altering the background color. You can use your own images and add/remove segments on the fly.

    What Could You Use UISegmented For In Your App?

    Let me know in the comments below. Be sure to link to your apps on iTunes if you have them up yet!

    Learn How To Make Your Own iPhone, iPad iOS apps!

    Buy Now

    If you are completely new to iPhone programming and want to start to developing iPhone apps this ebook will give you everything you need to get started today.

    You Get the 5 Essential Things You Need to Make iPhone Apps

    • C Procedural Programming
    • Objective-C Object Oriented Programming
    • Cocoa-Touch Model-View-Controller Design Pattern
    • Architecture of Real World iPhone Apps
    • 12 Step App Development Formula

    This ebook is much different than the other books out there: it really is a *system* that includes ONLY what you need to know to make iPhone applications. Not only that, but you will get some real insight on how to make a product and how to set up your code in the best way the first time.

    Plus, you will get plenty of bonuses including exclusive source code projects. For more information, head on over to the website for the ebook:

    Head over to How To Make An iPhone App eBook Website website to find out how to get Matt’s eBook and the bonus source code.

    Please share this if you like it!
    • Digg
    • del.icio.us
    • Facebook
    • Mixx
    • Google Bookmarks
    • email
    • FriendFeed
    • LinkedIn
    • MySpace
    • Ping.fm
    • StumbleUpon
    • Suggest to Techmeme via Twitter
    • Technorati
    • Tumblr
    • Yahoo! Bookmarks

    { 1 trackback }

    iPhoneKicks.com
    September 16, 2009 at 10:10 am

    Comments on this entry are closed.

    Previous post:

    Next post: