使用UISegmentedControl从一组或一组值中选择

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用UISegmentedControl从一组或一组值中选择相关的知识,希望对你有一定的参考价值。

The UISegmentedControl consists of a horizontal control divided into segments. Segmented controls are useful for allowing users to pick from a group or set of values.

Each segment functions as its own button. By default, selecting a segment will deselect the others in the control (much as a radio button does in html). You can alter this behavior by setting the "momentary" property.
  1. - (void)segmentClick:(id)sender
  2. {
  3. NSLog(@"you selected segment %d",[sender selectedSegmentIndex]);
  4. }
  5.  
  6. - (void)viewDidLoad {
  7.  
  8. [super viewDidLoad];
  9.  
  10. NSArray *arrSegments = [[NSArray alloc] initWithObjects:
  11. [NSString stringWithString:@"0"],
  12. [NSString stringWithString:@"1"],
  13. [NSString stringWithString:@"2"],nil];
  14.  
  15. UISegmentedControl *mySegment = [[UISegmentedControl alloc] initWithItems:arrSegments];
  16.  
  17. CGRect segmentRect = CGRectMake(10,50,300,40);
  18. [mySegment setFrame:segmentRect];
  19.  
  20. [mySegment addTarget:self action:@selector(segmentClick:) forControlEvents:UIControlEventValueChanged];
  21.  
  22. [mySegment setSegmentedControlStyle:UISegmentedControlStyleBar];
  23. [mySegment setTintColor:[UIColor darkGrayColor]];
  24. // mySegment.momentary = YES; // allow multiple multiple selection
  25.  
  26. //select first item
  27. [mySegment setSelectedSegmentIndex:0];
  28.  
  29. //change a segment size
  30. [mySegment setWidth:120.0 forSegmentAtIndex:1];
  31.  
  32. //add a new segment
  33. [mySegment insertSegmentWithTitle:@"new" atIndex:2 animated:YES];
  34.  
  35. //add segment to main view
  36. [self.view addSubview:mySegment];
  37.  
  38. }

以上是关于使用UISegmentedControl从一组或一组值中选择的主要内容,如果未能解决你的问题,请参考以下文章

7-指令系统

SQL语句——08聚集,分组,行转列

检查单元格中的特定字母或一组字母

UISegmentedControl去掉背景色与UIScrollView联动

iOS学习七之UISegmentedControl

如何使用 LINQ 从一组数字中查找 n 项的所有组合?