Here is How You Use UISegmentedControl

by mattjdrake on March 9, 2010

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:

 
icon for podpress  Here is How You Use UISegmentedControl: Play Now | Play in Popup | Download (229)

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!

    { 0 comments }

    Module 4 Released Early Today!

    by mattjdrake on February 21, 2010

    If you are subscribed to the Silver Program you will notice that I have released Module 4 – No-BS Cocoa-Touch with iPhone SDK a little bit early this month.

    Normally I like to release new modules on the 1st of the month but this week I will be in Washington DC teaching iPhone Boot Camp so I want to make sure you have your new content on time.  I am really excited about this module since it covers pretty much all the major patterns and techniques you need to really master iPhone (and iPad and iPod Touch) development.

    Here is the complete listing of modules available to you as of today:

    The iPhone App Development Mastery ECourse:

    Module 1 – Getting Started with iPhone App Development

    Module 2 – Learn How to Program in C

    Module 3 – Master Object Oriented Programming With Objective-C

    Module 4 – No-BS Cocoa-Touch with iPhone SDK

    Upcoming Modules

    Module 5 – Multipage Apps With UITableView & UINavigationController [COMING SOON!!!]

    { 0 comments }

    iPood: What The iPad Means For Mobile Apps

    February 4, 2010
    Thumbnail image for iPood: What The iPad Means For Mobile Apps

    In this week’s show I share my thoughts on Apple’s iPad announcement last week.

     
    icon for podpress  iPood: What The iPad Means For Mobile Apps: Play Now | Play in Popup | Download (537)

    Show Notes
    iPood
    You may have wondered why I missed the last podcast… Well, the reason is that I was meeting my new daughter: Keira Ruby! Just want to welcome her as the newest member of the mobile app family (iPood: there [...]

    Read the full article →

    Do you want to know how to get in on the red hot iPhone app development scene?

    January 22, 2010

    Today I Am Going To Show You How To Master iPhone App Development
    …see how to make a fantastic iPhone app even if you never programmed before!
    Who Am I & How Can I Help You?
    My name is Matt and I develop iPhone apps full-time for my own home based mobile app business.
    I’ve been at it since [...]

    Read the full article →

    Are U Ready To Start Your Mobile App Business?

    January 14, 2010
    Read the full article →

    Secret Feature #2 Unveiled!

    January 9, 2010
    Thumbnail image for Secret Feature #2 Unveiled!

    Today I finally finished our plans for the second secret feature that will come with the Silver Subscription that we will be releasing soon!
    Here is what comes with the silver subscription so far:
    iPhone App Development Mastery ECourse
    Code Chest
    Secret #2 Toolbox
    Secret Bonus
    What is ToolBox?
    Every day I get question from people asking me things like:

    How Do You [...]

    Read the full article →

    Episode 6: Do You Have Your Business Firewall Installed?

    January 7, 2010
    Thumbnail image for Episode 6: Do You Have Your Business Firewall Installed?

    In this week’s show I talk about my business goals for 2010, Nexus One and I answer a question from Andrew about Business Entities (your business firewall). Below you will find links to some of the business resources I mention in the podcast.

     
    icon for podpress  Episode 6: Do You Have Your Business Firewall Installed?: Play Now | Play in Popup | Download (647)

    Disclaimer: I am not a lawyer or an accountant and so you [...]

    Read the full article →

    Secret #1 Revealed Today!

    January 5, 2010
    Thumbnail image for Secret #1 Revealed Today!

    I thought that you would like to know that today I revealed the first secret feature that will be included with the iPhone App Development Mastery ECourse. This course will be released later this month to a small group of people. If you are interested in learning more about this exciting new feature click on [...]

    Read the full article →

    10 iPhone App Marketing Mistakes to Avoid

    January 4, 2010
    Thumbnail image for 10 iPhone App Marketing Mistakes to Avoid

    I’ve been fortunate enough in the last year to help Pinger promote 8 different apps into the top 100. I’ve learned as much from the mistakes we’ve made, as from the things we’ve done correctly.
    Guest Author: Brook Lenox
    [Read More]

    Read the full article →

    Sneak Peak: iPhone App Development Mastery E-Course

    December 23, 2009

    Are You Ready to Master App Development?

    Learn to become an iPhone OS ninja
    Complete beginners
    Programming 101 with C
    Objective-C Programming
    More advanced software developers
    NO-BS approach to mastering Cocoa-Touch
    Multi-Page Apps (UINavigationController/UITableView)
    Advanced UITableView
    Advanced UIKit
    Core Data
    Web Services
    [More to be announced]
    Why iPhone App Development?
    Consulting
    iPhone app developers are in high demand as more and more companies want to see their brand [...]

    Read the full article →