Ui: uisegmentedcontrol: simple option grouping and customization

Source: Internet
Author: User

Problem:

You can select one of the simple and easy-to-understand options.

 

Create a uisegmentedcontrol instance to initialize a control:
- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    self.view.backgroundColor = [UIColor whiteColor];    NSArray *segments = [[NSArray alloc]initWithObjects:                         @"iPhone",                         @"iPad",                         @"iPod",                         @"iMac", nil];     _mySegmentControl = [[UISegmentedControl alloc]initWithItems:segments];    _mySegmentControl.center = self.view.center;        [self.view addSubview:_mySegmentControl];}
Now, how do we know that the user has selected an option? The answer is simple. Just like uiswitch and uislider, use addtarget: Action: forcontrolevents: to add a target. you need to pass the uicontroleventvaluechanged value to the forcontrolevents parameter because this parameter triggers the event when you select an option in the control.
[_mySegmentControl addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
//segment选项变化时调用方法- (void)segmentChanged:(UISegmentedControl *)paramSender{    if ([paramSender isEqual:_mySegmentControl]) {        NSInteger selectedSegmentIndex = [paramSender selectedSegmentIndex];        NSString *selectedSegmentText = [paramSender titleForSegmentAtIndex:selectedSegmentIndex];        NSLog(@"Segment %ld with %@ text is selected", (long)selectedSegmentIndex,selectedSegmentText);    }}
If you want to change this option back to the original state (as if it was not selected) after you select an option, set the momentary attribute to yes.
_mySegmentControl.momentary = YES;
A very flexible feature of the segment control is that it can display images. To display an image, use initwithobjects: the initialization method transmits the image, character, and image as parameters at the same time.
NSArray *segments = [[NSArray alloc] initWithObjects: @"iPhone",[UIImage imageNamed:@"iPad.png"],@"iPod",@"iMac", nil];self.mySegmentedControl = [[UISegmentedControl alloc] initWithItems:segments];

 

 

Ui: uisegmentedcontrol: simple option grouping and customization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.