好了,接着总结上次没总结完的UI常用控件,下面分别是开关控件、分段控件、滑块空间、选择器控件、日期控件。当然还有两种控件自己自学去,分别是文本控件(UITextView)、网页显示控件(UIWebView)。

开关的使用

   

UISwitch *sw=[[UISwitch alloc]initWithFrame:CGRectMake(100, 65, 100, 50)];//创建switch,并设置大小
   addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];//创建事件
   [self.view addSubview:sw];//添加到父视图
   
   
   //UISegmentedControl的使用
   //设置标题和个数数组
   NSArray *ary=@[@"1",@"2",@"3",@"4"];
   UISegmentedControl *seg=[[UISegmentedControl alloc]initWithItems:ary];
   seg.frame=CGRectMake(50, 100, 200, 50);
   //设置seg的标题和图片,都起到类似标题的作用,没有改变点击后的背景效果
   setTitle:@"123" forSegmentAtIndex:0];
   setImage:[UIImage imageNamed:@"Airplane"] forSegmentAtIndex:0];
   
   
   //设置整个segde 背景图片
  
   
   //下面这是添加图片的时候所用方法,但不是添加背景图片,而是类似于标题的图片,用不熟练的话很困惑的。
  
   
  // seg.segmentedControlStyle=UISegmentedControlStylePlain;//设置样式
   
  segmentedControlStyle=UISegmentedControlStyleBar;
   //当样式设为bar时,这个时候,setTintColor才有效果,否则无效
   setTintColor:[UIColor redColor]];//seg的背景颜色
   setSelectedSegmentIndex:0];//首先显示index=0的内容
  momentary=NO;//设置在点击后是否恢复原样
  multipleTouchEnabled=NO;//可触摸
   
   addTarget:self action:@selector(selectBtn:) forControlEvents:UIControlEventValueChanged];//添加事件
   
   //创建集合,体现标题如果是文字的话,可以设置字号,字体,颜色等
   NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,[UIFont fontWithName:@"SnellRoundhand-Bold" size:24],UITextAttributeFont ,nil];
   setTitleTextAttributes:dic forState:UIControlStateNormal];
   
   [self.view addSubview:seg];
   
   
   
   //UISlider的使用
  UISlider *slider=[[UISlider alloc]initWithFrame:CGRectMake(50, 150, 150, 50)];//创建slider滑块,并指定位置大小
  backgroundColor=[UIColor clearColor];//背景颜色
   slider.minimumValue=0.2;//设置最小值
   slider.maximumValue=1;//设置最大值
   slider.value=1;//设置当前值
   self.view.alpha=1;//设置当前视图的透明度
   addTarget:self action:@selector(sder:) forControlEvents:UIControlEventValueChanged];//事件
   [self.view addSubview:slider];
   
   
   //UIPickerView的使用
   pickView=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 200, 320, 216)];
   
   pickView.delegate=self;//设置代理
   pickView.dataSource=self;
   pickView.showsSelectionIndicator=YES;
   
   contentAry=[[NSArray alloc]initWithObjects:@"水瓶座",@"双鱼座",@"天蝎座",@"白羊座",@"金牛座",@"双子座",@"巨蟹座",@"射手座",@"处女座",@"天秤座",@"狮子座",@"摩羯座", nil];
   lab1=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 58)];
   lab1.backgroundColor=[UIColor redColor];
   
   [self.view addSubview:lab1];
 
   [self.view  addSubview:pickView];
   
   //UIDatePicker日历控件的使用
   datePick=[[UIDatePicker alloc]initWithFrame:CGRectMake(0, 200, 320, 480)];//创建datepicker日历控件
   //设置日历控件显示日期为当前日期
  datePick.date=[NSDate date];
   datePick.datePickerMode=UIDatePickerModeDateAndTime;//日期选择模式
  self.view addSubview:datePick];
 
 
 
  
 
 
 
  
 
 
下面写的是执行的方法,自己可以参考一下。
 
 
返回显示的列数
//返回显示的列数
NSInteger)numberOfComponentsInPickerView:(UIPickerView
  return 1;
 }
返回当前显示的列数
//返回当前显示的列数,不用的话会崩溃
NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    lab1.text=@"水瓶座";//初始设置为显示数组的第一个元素,否则刚开始会默认为空
   return [contentAry count];
 }
 
设置当前列显示的行数
//设置当前列显示的行数,不用的话会显示问号
NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
  return [contentAry objectAtIndex:row];
 }
 
点击你所选择的行数
//点击你所选择的行数,不用里面的数据的话,lab1的内容不会改变。
void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
   NSString *result=nil;
  
   NSLog(@"result:%@",result);
  
 }
 
滑块控件sder事件
void)sder:(UISlider *)sender{
   //value 滑块控件的当前值,它在最大值和最小值之间0~1
   //minimumValue 滑块控件的最小值
   //maximumValue 滑块控件的最大值
  self.view.alpha=sender.value;//设置让当前页面的透明度等于滑块的当前值
 
 }
 
事件
void)selectBtn:(UISegmentedControl *)sender{
   //选择段下标
  int
   NSLog(@"%d,%@",index,[sender titleForSegmentAtIndex:index]);
 }
 
事件
void)change:(UISwitch *)sender{
   //on BOOL值,获取/设置开关状态
   //-(void)setON:(BOOL)on animated:(BOOL)animated 动画设置控件的开关状态
   //UIControlEventValueChanged 事件。用户点击开关控件,会触发此事件!
   if (sender.on) {//当BOOL值为on时,执行orangeColor
      self.view.backgroundColor=[UIColor orangeColor];
       
  else if(sender){//当BOOL值不为on时,执行grayColor
      self.view.backgroundColor=[UIColor grayColor];
       
  
   
 }