iphone常用控件之UISegmentControl

-(void) viewDidLoad{
/*************分段控件UISegmentdControl*************/ UISegmentedControl *segmentedControl = [[UISegmentedControlalloc] initWithItems:nil]; segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; //控件风格小按钮,适合导航栏: segmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled; //控件风格 segmentedControl.segmentedControlStyle = UISegmentedControlStyleBordered; //控件风格黑边的大白按钮,适合表格单元 segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain; //控件风格灰边大白按钮,适合偏好设置单元 segmentedControl.tintColor = [UIColor blueColor]; //渲染色彩 //添加片段,从0单元开始 ,可加入标题或图片两种情况 [segmentedControl insertSegmentWithTitle:@"First"atIndex:0animated:NO]; [segmentedControl insertSegmentWithTitle:@"Second"atIndex:1animated:NO]; UIImage *myImage = [UIImage imageNamed:@"1.png"]; [segmentedControl insertSegmentWithImage:myImage atIndex:2animated:NO]; //删除片段 [segmentedControl removeSegmentAtIndex:1 animated:YES]; //删除单个 [segmentedControl removeAllSegments]; //删除所有 //片段标题 [segmentedControl setTitle:@"Third" forSegmentAtIndex:2];//设置标题 NSString *myTitle = [segmentedControl titleForSegmentAtIndex:0];//读取标题 //图像 [segmentedControl setImage:[UIImageimageNamed:@"2.png"] forSegmentAtIndex:0];//设置图像 UIImage *myImage = [segmentedControl imageForSegmentAtIndex:3]; //读取图片 //设置片段宽度 [segmentedControl setWidth:64.0 forSegmentAtIndex:0]; //瞬时单击 segmentedControl.momentary = YES; //按钮被按下后很快恢复,默认为选中状态就一直保持 //初始化默认片段 segmentedControl.selectedSegmentIndex = 0; //初始指定第0个选中 //显示控件 [parentView addSubview:segmentedControl]; //添加到父视图 self.navigationItem.titleView = segmentedControl; //添加到导航栏(通过视图控制器) //读取控件 int selectIndex = segmentedControl.selectedSegmentIndex; //对应当前被选总的片段号码 [segmentedControl addTarget:selfaction:@selector(controlPressed:) forControlEvents:UIControlEventValueChanged];

}

 //SegmentedControl触发的动作    

-(void)controlPressed:(id)sender{        

        UISegmentedControl *control = (UISegmentedControl *)sender;

        if (control == mySegmentedControl) {            

            int x = control.selectedSegmentIndex;           

            /*添加代码,对片段变化做出响应*/         

    }

}

 
posted @ 2012-08-19 16:28  小、  阅读(7303)  评论(0编辑  收藏  举报