关闭

iOS控件之UISegmentedControl

245人阅读 评论(0) 收藏 举报
分类:
分段控件UISegmentedControl

一,作用:分段控件,页面的切换等等


二,UISegmentedControlDemo

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.window.backgroundColor = [UIColor whiteColor];
    
//    NSArray *items = @[@"搜索", @"图片", @"视频", @"音乐"];
    NSArray *images = @[[UIImage imageNamed:@"segment_check"], [UIImage imageNamed:@"segment_search"], [UIImage imageNamed:@"segment_tools"]];
    UISegmentedControl *sc = [[UISegmentedControl alloc] initWithItems:images];
    sc.segmentedControlStyle = UISegmentedControlStylePlain;
    sc.frame = CGRectMake(60, 100, 200, 40);
    sc.selectedSegmentIndex = 1;
//    sc.tintColor = [UIColor redColor];
    [sc addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
    [self.window addSubview:sc];
    [sc release];
    
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)change:(UISegmentedControl *)sc
{
    NSLog(@"sc : %d", sc.selectedSegmentIndex);
}




  1. #import "SegmentedControlTestViewController.h"  
  2. @implementation SegmentedControlTestViewController  
  3. @synthesize segmentedControl;  
  4.   
  5.   
  6. // Implement viewDidLoad to do additional setup after loading the view, typically from nib.  
  7. (void)viewDidLoad  
  8.     NSArray *segmentedArray [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",nil];  
  9.     //初始化UISegmentedControl  
  10.     UISegmentedControl *segmentedTemp [[UISegmentedControl alloc]initWithItems:segmentedArray];  
  11.     segmentedControl segmentedTemp;  
  12.     segmentedControl.frame CGRectMake(60.0, 10.0, 200.0, 50.0);  
  13.       
  14.     [segmentedControl setTitle:@"two" forSegmentAtIndex:1];//设置指定索引的题目  
  15.     [segmentedControl setImage:[UIImage imageNamed:@"lan.png"forSegmentAtIndex:3];//设置指定索引的图片  
  16.     [segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"mei.png"atIndex:2 animated:NO];//在指定索引插入一个选项并设置图片  
  17.     [segmentedControl insertSegmentWithTitle:@"insert" atIndex:3 animated:NO];//在指定索引插入一个选项并设置题目  
  18.     [segmentedControl removeSegmentAtIndex:0 animated:NO];//移除指定索引的选项  
  19.     [segmentedControl setWidth:70.0 forSegmentAtIndex:2];//设置指定索引选项的宽度  
  20.     [segmentedControl setContentOffset:CGSizeMake(10.0,10.0) forSegmentAtIndex:1];//设置选项中图片等的左上角的位置  
  21.       
  22.     //获取指定索引选项的图片imageForSegmentAtIndex:  
  23.     UIImageView *imageForSegmentAtIndex [[UIImageView alloc]initWithImage:[segmentedControl imageForSegmentAtIndex:1]];  
  24.     imageForSegmentAtIndex.frame CGRectMake(60.0, 100.0, 30.0, 30.0);  
  25.       
  26.     //获取指定索引选项的标题titleForSegmentAtIndex  
  27.     UILabel *titleForSegmentAtIndex [[UILabel alloc]initWithFrame:CGRectMake(100.0, 100.0, 30.0, 30.0)];  
  28.     titleForSegmentAtIndex.text [segmentedControl titleForSegmentAtIndex:0];  
  29.       
  30.     //获取总选项数segmentedControl.numberOfSegments  
  31.     UILabel *numberOfSegments [[UILabel alloc]initWithFrame:CGRectMake(140.0, 100.0, 30.0, 30.0)];  
  32.     numberOfSegments.text [NSString stringWithFormat:@"%d",segmentedControl.numberOfSegments];  
  33.       
  34.     //获取指定索引选项的宽度widthForSegmentAtIndex:  
  35.     UILabel *widthForSegmentAtIndex [[UILabel alloc]initWithFrame:CGRectMake(180.0, 100.0, 70.0, 30.0)];  
  36.     widthForSegmentAtIndex.text [NSString stringWithFormat:@"%f",[segmentedControl widthForSegmentAtIndex:2]];  
  37.       
  38.     segmentedControl.selectedSegmentIndex 2;//设置默认选择项索引  
  39.     segmentedControl.tintColor [UIColor redColor];  
  40.     segmentedControl.segmentedControlStyle UISegmentedControlStylePlain;//设置样式  
  41.     segmentedControl.momentary YES;//设置在点击后是否恢复原样  
  42.       
  43.     [segmentedControl setEnabled:NO forSegmentAtIndex:4];//设置指定索引选项不可选  
  44.     BOOL enableFlag [segmentedControl isEnabledForSegmentAtIndex:4];//判断指定索引选项是否可选  
  45.     NSLog(@"%d",enableFlag);  
  46.       
  47.     [self.view addSubview:widthForSegmentAtIndex];  
  48.     [self.view addSubview:numberOfSegments];  
  49.     [self.view addSubview:titleForSegmentAtIndex];  
  50.     [self.view addSubview:imageForSegmentAtIndex];  
  51.     [self.view addSubview:segmentedControl];  
  52.       
  53.     [widthForSegmentAtIndex release];  
  54.     [numberOfSegments release];  
  55.     [titleForSegmentAtIndex release];  
  56.     [segmentedTemp release];  
  57.     [imageForSegmentAtIndex release];  
  58.       
  59.     //移除所有选项  
  60.     //[segmentedControl removeAllSegments];  
  61.     [super viewDidLoad];  
  62.  
  63.   
  64.   
  65. (void)didReceiveMemoryWarning  
  66.     // Releases the view if it doesn't have superview.  
  67.     [super didReceiveMemoryWarning];  
  68.       
  69.     // Release any cached data, images, etc that aren't in use.  
  70.  
  71. (void)viewDidUnload  
  72.     // Release any retained subviews of the main view.  
  73.     // e.g. self.myOutlet nil;  
  74.  
  75.   
  76. (void)dealloc  
  77.     [segmentedControl release];  
  78.     [super dealloc];  
  79.  
  80. @end

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:56589次
    • 积分:1527
    • 等级:
    • 排名:千里之外
    • 原创:91篇
    • 转载:31篇
    • 译文:1篇
    • 评论:3条
    博客专栏
    最新评论