iOS create UISegmentedControl programmatically

A segmented control is a UI Component that display a series of buttons that are stuck to each other and displayed horizontally. You can relate this to a group of radio buttons on a HTML form where the user can make a choice. In this example we will learn the following
  • Create and initialize a UISegmentControl from an array list of Items
  • Set style for the segmented control
  • Default a specific segment as selected
  • Remove a segment from the control
  • Add a new segment to the control
  • Attach a target action to the control to capture the selection made by the user
  • You can make the control work like a UIButton that pops back up by setting the momentary property to TRUE

iOS create UISegmentedControl programmatically
iOS UISegmentedControl console log

Interface file for the view controller - MySegmentedButtonViewController.h

#import <UIKit/UIKit.h>

@interface MySegmentedButtonViewController : UIViewController

@property (nonatomic, strong) UISegmentedControl *mySegmentedControl;

@end

Implementation file for the view controller - MySegmentedButtonViewController.m

#import "MySegmentedButtonViewController.h"

@interface MySegmentedButtonViewController ()

@end

@implementation MySegmentedButtonViewController

@synthesize mySegmentedControl;

- (void)viewDidLoad
{
    [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
    
    //set the view background to white
    self.view.backgroundColor = [UIColor whiteColor];
    
    //frame for the segemented button
    CGRect myFrame = CGRectMake(10.0f, 10.0f, 300.0f, 40.0f);
    
    //Array of items to go inside the segment control
    //You can choose to add an UIImage as one of the items instead of NSString
    NSArray *mySegments = [[NSArray alloc] initWithObjects: @"Red",
                         @"Blue", @"Green", @"Yellow", nil];
    
    //create an intialize our segmented control
    self.mySegmentedControl = [[UISegmentedControl alloc] initWithItems:mySegments];
    
    //set the size and placement
    self.mySegmentedControl.frame = myFrame;
    
    //set the style for the segmented control
    self.mySegmentedControl.segmentedControlStyle  = UISegmentedControlStyleBar;
    
    //remove the third index from the control
    [self.mySegmentedControl removeSegmentAtIndex:2
                                         animated:true];
    
    //add another control at the end
    [self.mySegmentedControl insertSegmentWithTitle:@"Brown"
                                        atIndex:3
                                        animated:true];

    
    //default the selection to second item
    [self.mySegmentedControl setSelectedSegmentIndex:1];
    
    //attach target action for if the selection is changed by the user
    [self.mySegmentedControl addTarget:self
                                action:@selector(whichColor:)
                                forControlEvents:UIControlEventValueChanged];
    
    //add the control to the view
    [self.view addSubview:self.mySegmentedControl];
    
}

- (void) whichColor:(UISegmentedControl *)paramSender{
    
    //check if its the same control that triggered the change event
    if ([paramSender isEqual:self.mySegmentedControl]){
    
        //get index position for the selected control
        NSInteger selectedIndex = [paramSender selectedSegmentIndex];
        
        //get the Text for the segmented control that was selected
        NSString *myChoice =
                    [paramSender titleForSegmentAtIndex:selectedIndex];
        //let log this info to the console
        NSLog(@"Segment at position %i with %@ text is selected",
              selectedIndex, myChoice);
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Reference

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.