How to use the Segmented Control

Here is How You Use the Segmented Control [UISegmentedControl]

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:

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:

#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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值