How to use the Segmented Control
来源:互联网 发布:数据恢复精灵有没有用 编辑:程序博客网 时间:2024/05/21 09:57
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 UseSegmentedControlViewControllerUILabel *label;- (void)viewDidLoad { [super viewDidLoad];//Create labellabel = [[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 UseSegmentedControlViewControllerUILabel *label;- (void)viewDidLoad { [super viewDidLoad];//Create labellabel = [[UILabel alloc] init];label.frame = CGRectMake(10, 10, 300, 40);label.textAlignment = UITextAlignmentCenter;[self.view addSubview:label]; //Create the segmented controlNSArray *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!
- How to use the Segmented Control
- How to use softvol to control the master volume
- How to use softvol to control the master volume
- How to use .NET Windows Control into the WPF Application
- How to use ReportViewer Control
- how to use javascript to control the usercotrol in the asp.net
- How to use the CAknWaitDialog
- how to use the CAknwaitDialog
- How to use the CAknWaitDialog
- How to use the BAPI_BUPA_ADDRESS_CHANGE
- Python use Selenium to control the webdriver
- JS: How to control the IE(keyDown...)?
- 权限管理 how to control the authorization?
- How to control the jobs with shell?
- how to control the log4j output
- How to use the HttpWebRequest object
- How To Use the C Run-Time
- How to Use the Visual C++ DEBUGGER
- B-树(程序)
- V8引擎实现标准ECMA-262(二)
- Linux服务器关闭SeLinux的方法
- 面向接口编程详解(三)——模式研究
- 解决“要运行Visual Studio 2005 sp1,建议使用管理员权限”的问题
- How to use the Segmented Control
- 解读Google分布式锁服务
- 不重启Linux服务器关闭SeLinux的方法
- linux 安装
- 据说看完这21个故事的人,30岁前都成了亿万富翁。你是下一个吗?
- 辞旧迎新
- linux下安装oracle 10g
- 串
- Hibernate之缓存机制(一级缓存)