UISegmentedControl详解

#import "SegmentedControlTestViewController.h"  
    @implementation SegmentedControlTestViewController  
    @synthesize segmentedControl;  
      
    /**************************************** 
     Tasks 
    Initializing a Segmented Control 
     – initWithItems:   
    Managing Segment Content 
     – setImage:forSegmentAtIndex:   
     – imageForSegmentAtIndex:   
     – setTitle:forSegmentAtIndex:   
     – titleForSegmentAtIndex:   
    Managing Segments 
     – insertSegmentWithImage:atIndex:animated:   
     – insertSegmentWithTitle:atIndex:animated:   
     numberOfSegments  property   
     – removeAllSegments   
     – removeSegmentAtIndex:animated:   
     selectedSegmentIndex  property   
    Managing Segment Behavior and Appearance 
     momentary  property   
     segmentedControlStyle  property   
     tintColor  property   
     – setEnabled:forSegmentAtIndex:   
     – isEnabledForSegmentAtIndex:   
     – setContentOffset:forSegmentAtIndex:   
     – contentOffsetForSegmentAtIndex:   
     – setWidth:forSegmentAtIndex:   
     – widthForSegmentAtIndex:   
    *********************************************/  
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.  
    - (void)viewDidLoad {  
        NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",nil];  
        //初始化UISegmentedControl  
        UISegmentedControl *segmentedTemp = [[UISegmentedControl alloc]initWithItems:segmentedArray];  
        segmentedControl = segmentedTemp;  
        segmentedControl.frame = CGRectMake(60.0, 10.0, 200.0, 50.0);  
          
        [segmentedControl setTitle:@"two" forSegmentAtIndex:1];//设置指定索引的题目  
        [segmentedControl setImage:[UIImage imageNamed:@"lan.png"] forSegmentAtIndex:3];//设置指定索引的图片  
        [segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"mei.png"] atIndex:2 animated:NO];//在指定索引插入一个选项并设置图片  
        [segmentedControl insertSegmentWithTitle:@"insert" atIndex:3 animated:NO];//在指定索引插入一个选项并设置题目  
        [segmentedControl removeSegmentAtIndex:0 animated:NO];//移除指定索引的选项  
        [segmentedControl setWidth:70.0 forSegmentAtIndex:2];//设置指定索引选项的宽度  
        [segmentedControl setContentOffset:CGSizeMake(10.0,10.0) forSegmentAtIndex:1];//设置选项中图片等的左上角的位置  
          
        //获取指定索引选项的图片imageForSegmentAtIndex:  
        UIImageView *imageForSegmentAtIndex = [[UIImageView alloc]initWithImage:[segmentedControl imageForSegmentAtIndex:1]];  
        imageForSegmentAtIndex.frame = CGRectMake(60.0, 100.0, 30.0, 30.0);  
          
        //获取指定索引选项的标题titleForSegmentAtIndex  
        UILabel *titleForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(100.0, 100.0, 30.0, 30.0)];  
        titleForSegmentAtIndex.text = [segmentedControl titleForSegmentAtIndex:0];  
          
        //获取总选项数segmentedControl.numberOfSegments  
        UILabel *numberOfSegments = [[UILabel alloc]initWithFrame:CGRectMake(140.0, 100.0, 30.0, 30.0)];  
        numberOfSegments.text = [NSString stringWithFormat:@"%d",segmentedControl.numberOfSegments];  
          
        //获取指定索引选项的宽度widthForSegmentAtIndex:  
        UILabel *widthForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(180.0, 100.0, 70.0, 30.0)];  
        widthForSegmentAtIndex.text = [NSString stringWithFormat:@"%f",[segmentedControl widthForSegmentAtIndex:2]];  
          
        segmentedControl.selectedSegmentIndex = 2;//设置默认选择项索引  
        segmentedControl.tintColor = [UIColor redColor];  
        segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;//设置样式  
        segmentedControl.momentary = YES;//设置在点击后是否恢复原样  
          
        [segmentedControl setEnabled:NO forSegmentAtIndex:4];//设置指定索引选项不可选  
        BOOL enableFlag = [segmentedControl isEnabledForSegmentAtIndex:4];//判断指定索引选项是否可选  
        NSLog(@"%d",enableFlag);  
          
        [self.view addSubview:widthForSegmentAtIndex];  
        [self.view addSubview:numberOfSegments];  
        [self.view addSubview:titleForSegmentAtIndex];  
        [self.view addSubview:imageForSegmentAtIndex];  
        [self.view addSubview:segmentedControl];  
          
        [widthForSegmentAtIndex release];  
        [numberOfSegments release];  
        [titleForSegmentAtIndex release];  
        [segmentedTemp release];  
        [imageForSegmentAtIndex release];  
          
        //移除所有选项  
        //[segmentedControl removeAllSegments];  
        [super viewDidLoad];  
    }  
      
    /* 
    // Override to allow orientations other than the default portrait orientation. 
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
        // Return YES for supported orientations 
        return (interfaceOrientation == UIInterfaceOrientationPortrait); 
    } 
    */  
    - (void)didReceiveMemoryWarning {  
        // Releases the view if it doesn't have a superview.  
        [super didReceiveMemoryWarning];  
          
        // Release any cached data, images, etc that aren't in use.  
    }  
    - (void)viewDidUnload {  
        // Release any retained subviews of the main view.  
        // e.g. self.myOutlet = nil;  
    }  
      
    - (void)dealloc {  
        [segmentedControl release];  
        [super dealloc];  
    }  
    @end

UISegmentedControl分段按钮的使用  

2011-08-02 18:01:27|  分类: IOS|字号 订阅

文章来源:http://blog.sealyu.com/2011/01/01/uisegmentedcontrol%E5%88%86%E6%AE%B5%E6%8C%89%E9%92%AE%E7%9A%84%E4%BD%BF%E7%94%A8-%E9%83%9D%E6%97%AD/



实现如下代码:

- (void)viewDidLoad {
[super viewDidLoad];
segmentedControl=[[UISegmentedControl alloc] initWithFrame:CGRectMake(80.0f, 8.0f, 70.0f, 25.0f) ];
[segmentedControl insertSegmentWithTitle:@"T+" atIndex:0 animated:YES];
[segmentedControl insertSegmentWithTitle:@"T-" atIndex:1 animated:YES];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
segmentedControl.multipleTouchEnabled=NO;
[segmentedControl addTarget:self action:@selector(Selectbutton:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *segButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segButton;
[segButton release];

}

-(void)Selectbutton:(int)sender{
UISegmentedControl *myUISegmentedControl=(UISegmentedControl *)sender;
NSLog(@”!!!!!!!!%d”,myUISegmentedControl.selectedSegmentIndex);
switch (myUISegmentedControl.selectedSegmentIndex) {
case 1:
break;
case 0:
break;
default:
break;
}
}

segmentedControl 中的字体也是可以改动的,代码如下:

-(void)ChangeSegmentFont:(UIView *)aView
{
if ([aView isKindOfClass:[UILabel class]]) {
UILabel *lb = (UILabel    *)aView;
[lb setTextAlignment:UITextAlignmentCenter];
[lb setFrame:CGRectMake(0, 0, 40, 25)];
[lb setFont:[UIFont systemFontOfSize:12]];
}
NSArray *na = [aView subviews];
NSEnumerator *ne = [na objectEnumerator];
UIView *subView;
while (subView = [ne nextObject]) {
[self ChangeSegmentFont:subView];
}
}

在需要调用此方法的地方

[self ChangeSegmentFont:UISegmentedControl的对象];

即可

当用户输入不仅仅是布尔值时,可使用分段控件(UISegmentedControl)。分段控件提供一栏按钮(有时称为按钮栏),但只能激活其中一个按钮。分段控件会导致用户在屏幕上看到的内容发生变化。它们常用于在不同类别的信息之间选择,或在不同的应用屏幕之间切换。下面介绍基本属性和基本方法的使用。

[代码]c#/cpp/oc代码:

1 NSArray *segmentedArray = [[NSArrayalloc]initWithObjects:@"1",@"2",@"3",@"4",nil]; 
2     //初始化UISegmentedControl 
3     UISegmentedControl *segmentedControl = [[UISegmentedControlalloc]initWithItems:segmentedArray]; 
4     segmentedControl.frame = CGRectMake(20.0, 20.0, 250.0, 50.0); 
5     segmentedControl.selectedSegmentIndex = 2;//设置默认选择项索引 
6     segmentedControl.tintColor = [UIColor redColor]; 
7   //有基本四种样式
8     segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;//设置样式

[代码]c#/cpp/oc代码:

1 //segmentedControl.segmentedControlStyle = UISegmentedControlStyleBordered;//设置样式

[代码]c#/cpp/oc代码:

1 //segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;//设置样式

[代码]c#/cpp/oc代码:

1 //segmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled;//设置样式

[代码]c#/cpp/oc代码:

1 //  segmentedControl.momentary = YES;//设置在点击后是否恢复原样    
2     [segmentedControl setTitle:@"two" forSegmentAtIndex:1];//设置指定索引的题目

[代码]c#/cpp/oc代码:

1 [segmentedControl setImage:[UIImage imageNamed:@"btn_jyy.png"] forSegmentAtIndex:3];//设置指定索引的图片

[代码]c#/cpp/oc代码:

1 [segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"mei.png"] atIndex:2 animated:NO];//在指定索引插入一个选项并设置图片

[代码]c#/cpp/oc代码:

1 [segmentedControl insertSegmentWithTitle:@"insert" atIndex:3 animated:NO];//在指定索引插入一个选项并设置题目

[代码]c#/cpp/oc代码:

1 [segmentedControl removeSegmentAtIndex:0 animated:NO];//移除指定索引的选项

[代码]c#/cpp/oc代码:

01 //   [segmentedControl setWidth:70.0 forSegmentAtIndex:2];//设置指定索引选项的宽度 
02  //   [segmentedControl setContentOffset:CGSizeMake(10.0,10.0) forSegmentAtIndex:4];//设置选项中图片等的左上角的位置 
03      
04     //获取指定索引选项的图片imageForSegmentAtIndex: 
05     UIImageView *imageForSegmentAtIndex = [[UIImageViewalloc]initWithImage:[segmentedControl imageForSegmentAtIndex:1]]; 
06     imageForSegmentAtIndex.frame = CGRectMake(60.0, 120.0, 30.0, 30.0);  ;
07      
08     //获取指定索引选项的标题titleForSegmentAtIndex 
09     UILabel *titleForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(100.0, 160.0, 30.0, 30.0)]; 
10     titleForSegmentAtIndex.text = [segmentedControl titleForSegmentAtIndex:0]; 
11      
12     //获取总选项数segmentedControl.numberOfSegments 
13     UILabel *numberOfSegments = [[UILabel alloc]initWithFrame:CGRectMake(140.0, 170.0, 30.0, 30.0)]; 
14     numberOfSegments.text = [NSString stringWithFormat:@"%d",segmentedControl.numberOfSegments];
15   
16     //获取指定索引选项的宽度widthForSegmentAtIndex: 
17     UILabel *widthForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(180.0, 210.0, 70.0, 30.0)]; 
18     widthForSegmentAtIndex.text = [NSString stringWithFormat:@"%f",[segmentedControl widthForSegmentAtIndex:2]]; 
19       
20    // [segmentedControl setEnabled:NO forSegmentAtIndex:4];//设置指定索引选项不可选 
21    // BOOL enableFlag = [segmentedControl isEnabledForSegmentAtIndex:4];//判断指定索引选项是否可选 
22       [mySegmentedControladdTarget:selfaction:@selector(segmentAction:)forControlEvents:UIControlEventValueChanged]; //添加委托方法
23 //具体委托方法实例  
24 -(void)segmentAction:(UISegmentedControl *)Seg{
25     NSInteger Index = Seg.selectedSegmentIndex;
26     NSLog(@"Index %i", Index);
27     switch (Index) {
28         case 0:
29             [self selectmyView1];
30             break;
31         case 1:
32             [self selectmyView2];
33             break;
34         case 2:
35             [self selectmyView3];
36             break;
37         case 3:
38             [self selectmyView4];
39             break;
40         case 4:
41             [self selectmyView5];
42             break;
43         case 5:
44             [self selectmyView6];
45             break;   
46         default:
47             break;
48     }
49 }

基本属性、方法如上,大家在实际开发中可根据实际需求选取适当属性和方法。

 

使用技巧:

在导航栏中添加UISegmentedControl ,实现的效果:

UISegmentedControl详解_第1张图片

实现的代码:

[代码]c#/cpp/oc代码:

view source
print ?
01 //自定义UISegmentedcontrol
02 UISegmentedControl *segmentedControl=[[UISegmentedControl alloc] initWithFrame:CGRectMake(80.0f, 8.0f, 200.0f, 30.0f) ];
03     [segmentedControl insertSegmentWithTitle:@"Food to eat" atIndex:0 animated:YES];
04     [segmentedControl insertSegmentWithTitle:@"Food to avoid" atIndex:1 animated:YES];
05     segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
06     segmentedControl.momentary = YES;
07     segmentedControl.multipleTouchEnabled=NO;
08     [segmentedControl addTarget:self action:@selector(Selectbutton:) forControlEvents:UIControlEventValueChanged];
09     UIBarButtonItem *segButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl]; //自定义UIBarButtonItem,封装定义好的UIsegmented。
10     [segmentedControl release];
11     self.navigationItem.rightBarButtonItem = segButton;  //添加到导航栏中
12     [segButton release];


你可能感兴趣的:(UISegmentedControl详解)