iphone开发之UISegmentedControl 学习随笔

原文地址:iphone开发之UISegmentedControl 学习随笔 作者:若水一叶
//代码创建UISegmentedControl及基本设置
//自己代码中常用的
 
                UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithFrame:CGRectMake(120, 4, 150,40)];             
                  [segmentedControl insertSegmentWithTitle:@"first" atIndex:0animated:YES];
                  [segmentedControl insertSegmentWithTitle:@"second" atIndex:1animated:YES];
                  segmentedControl.segmentedControlStyle =UISegmentedControlStyleB ordered;//设置样式
                  [segmentedControl setSelectedSegmentIndex:0];//首先显示index=0的内容
                  segmentedControl.momentary =YES;      //设置在点击后是否恢复原样
                  segmentedControl.multipleTouchEnabled=NO;        //可触摸
                  [segmentedControl addTarget:self action:@selector(Selectbutton:)forControlEvents:UIControlEventValueChang ed];
                  UIBarButtonItem *segButton = [[UIBarButtonItem alloc]initWithCustomView:segmentedControl];
                  [segmentedControl release];
            //将UISegmentedControl添加到UINavigationController上
                  self.navigationItem.rightBarButtonItem = segButton;                             
//其他代码,别人的
  1. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.  
  2. - (void)viewDidLoad {  
  3.     NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",nil];  
  4.     //初始化UISegmentedControl  
  5.     UISegmentedControl *segmentedTemp = [[UISegmentedControl alloc]initWithItems:segmentedArray];  
  6.     segmentedControl = segmentedTemp;  
  7.     segmentedControl.frame = CGRectMake(60.0, 10.0, 200.0, 50.0);  
  8.       
  9.     [segmentedControl setTitle:@"two" forSegmentAtIndex:1];//设置指定索引的题目  
  10.     [segmentedControl setImage:[UIImage imageNamed:@"lan.png"] forSegmentAtIndex:3];//设置指定索引的图片  
  11.     [segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"mei.png"] atIndex:2 animated:NO];//在指定索引插入一个选项并设置图片  
  12.     [segmentedControl insertSegmentWithTitle:@"insert" atIndex:3 animated:NO];//在指定索引插入一个选项并设置题目  
  13.     [segmentedControl removeSegmentAtIndex:0 animated:NO];//移除指定索引的选项  
  14.     [segmentedControl setWidth:70.0 forSegmentAtIndex:2];//设置指定索引选项的宽度  
  15.     [segmentedControl setContentOffset:CGSizeMake(10.0,10.0) forSegmentAtIndex:1];//设置选项中图片等的左上角的位置  
  16.       
  17.     //获取指定索引选项的图片imageForSegmentAtIndex:  
  18.     UIImageView *imageForSegmentAtIndex = [[UIImageView alloc]initWithImage:[segmentedControl imageForSegmentAtIndex:1]];  
  19.     imageForSegmentAtIndex.frame = CGRectMake(60.0, 100.0, 30.0, 30.0);  
  20.       
  21.     //获取指定索引选项的标题titleForSegmentAtIndex  
  22.     UILabel *titleForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(100.0, 100.0, 30.0, 30.0)];  
  23.     titleForSegmentAtIndex.text = [segmentedControl titleForSegmentAtIndex:0];  
  24.       
  25.     //获取总选项数segmentedControl.numberOfSegments  
  26.     UILabel *numberOfSegments = [[UILabel alloc]initWithFrame:CGRectMake(140.0, 100.0, 30.0, 30.0)];  
  27.     numberOfSegments.text = [NSString stringWithFormat:@"%d",segmentedControl.numberOfSegments];  
  28.       
  29.     //获取指定索引选项的宽度widthForSegmentAtIndex:  
  30.     UILabel *widthForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(180.0, 100.0, 70.0, 30.0)];  
  31.     widthForSegmentAtIndex.text = [NSString stringWithFormat:@"%f",[segmentedControl widthForSegmentAtIndex:2]];  
  32.       
  33.     segmentedControl.selectedSegmentIndex = 2;//设置默认选择项索引  
  34.     segmentedControl.tintColor = [UIColor redColor];  
  35.     segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;//设置样式  
  36.     segmentedControl.momentary = YES;//设置在点击后是否恢复原样  
  37.       
  38.     [segmentedControl setEnabled:NO forSegmentAtIndex:4];//设置指定索引选项不可选  
  39.     BOOL enableFlag = [segmentedControl isEnabledForSegmentAtIndex:4];//判断指定索引选项是否可选  
  40.     NSLog(@"%d",enableFlag);  
  41.       
  42.     [self.view addSubview:widthForSegmentAtIndex];  
  43.     [self.view addSubview:numberOfSegments];  
  44.     [self.view addSubview:titleForSegmentAtIndex];  
  45.     [self.view addSubview:imageForSegmentAtIndex];  
  46.     [self.view addSubview:segmentedControl];  
  47.       
  48.     [widthForSegmentAtIndex release];  
  49.     [numberOfSegments release];  
  50.     [titleForSegmentAtIndex release];  
  51.     [segmentedTemp release];  
  52.     [imageForSegmentAtIndex release];  
  53.       
  54.     //移除所有选项  
  55.     //[segmentedControl removeAllSegments];  
  56.     [super viewDidLoad];  
  57. }

你可能感兴趣的:(iPhone,action)