ios的UISwitch

来源:互联网 发布:教育管理硕士 知乎 编辑:程序博客网 时间:2024/05/03 17:10

正如分段控件代替了单选按钮,开关也代替了点选框。开关是到目前为止用起来最简单的控件,不过仍然可以作一定程度的定制化。

一、创建

 

[java] view plaincopyprint?
  1. UISwitch* mySwitch [[ UISwitch alloc]initWithFrame:CGRectMake(200.0,10.0,0.0,0.0)];  
是不是很奇怪,大小竟然是0.0×0.0,没错,系统会自动帮你决定最佳的尺寸,你自己写的尺寸会被忽略掉,你只要定义好相对父视图的位置就好了。关于纯代码创建控件请参看我的另一篇博文:《有关View的几个基础知识点》

 

二、显示控件

 

[java] view plaincopyprint?
  1. parrentView addSubview:mySwitch];//添加到父视图  

 

 

[java] view plaincopyprint?
  1. self.navigationItem.titleView mySwitch;//添加到导航栏  
二、开关状态

 

开关状态可以通过它的on属性读取,这个属性是一个BOOL值,表示开关是否被打开:

 

[java] view plaincopyprint?
  1. BOOL switchStatus mySwitch.on;  
你可以在你的代码中用setOn方法来打开或关闭开关:

 

 

[java] view plaincopyprint?
  1. mySwitch setOn:YES animated:YES];  

三、通知

 

想要在开关状态切换时收到通知,可以用UIControl类的addTarget方法为UIControlEventValueChanged事件添加一个动作。

 

 

[java] view plaincopyprint?
  1. mySwitch addTarget: self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];  

 

   这样,只要开关一被切换目标类(上例中目标类就是当前控制器self)就会调用switchValueChanged方法,是不是很棒呢?

[java] view plaincopyprint?
  1. (voidswitchValueChanged:(id)sender{  
  2.         UISwitch* control (UISwitch*)sender;  
  3.          if(control == mySwitch){  
  4.             BOOL on control.on;  
  5.      //添加自己要处理的事情代码  
  6.            
  7.  

了解了开关之后是不是觉得很棒呢?是不是发现有好多地方可以用到它?觉得不错就马上写点代码体验一下吧!



iphone 自定义UISwitch

修改UISwitch的标题,实现自定义UISwitch方法有两种: 

1.使用类别扩展UISwitch。 

如下:

 下面是UISwitch.h文件:

 

#import 

 

@interface UISwitch (tagged)

+ (UISwitch *) switchWithLeftText: (NSString *) tag1 andRight: (NSString *) tag2;

@property (nonatomicreadonly) UILabel *label1;

@property (nonatomicreadonly) UILabel *label2;

@end

 

UISwitch.m文件:

#import "UISwitch-Extended.h"

 

#define TAG_OFFSET 900

 

@implementation UISwitch (tagged)

- (void) spelunkAndTag: (UIView *) aView withCount:(int *) count

{

 for (UIView *subview in [aView subviews])

 {

 if ([subview isKindOfClass:[UILabel class]])

 {

 *count += 1;

 [subview setTag:(TAG_OFFSET + *count)];

 }

 else 

 [self spelunkAndTag:subview withCount:count];

 }

}

 

- (UILabel *) label1 

 return (UILabel *) [self viewWithTag:TAG_OFFSET 1]; 

}

 

- (UILabel *) label2 

return (UILabel *) [self viewWithTag:TAG_OFFSET 2]; 

}

 

+ (UISwitch *) switchWithLeftText: (NSString *) tag1 andRight: (NSString *) tag2

{

 UISwitch *switchView = [[UISwitch allocinitWithFrame:CGRectZero];

 

int labelCount = 0;

[switchView spelunkAndTag:switchView withCount:&labelCount];

 

if (labelCount == 2)

{

[switchView.label1 setText:tag1];

[switchView.label2 setText:tag2];

}

 

return [switchView autorelease];

}

 

@end

 

2.还有一种方法,这种方法比较简单,但比较难懂,我不甚理解。

 


 

 

UISwitch *isFooOrBar=[[UISwitch allocinit];

 

 

 


((UILabel *)[[[[[[isFooOrBar subviewslastObjectsubviewsobjectAtIndex:2subviews]objectAtIndex:0]).text @"Foo";

((UILabel *)[[[[[[isFooOrBar subviewslastObjectsubviewsobjectAtIndex:2subviews]objectAtIndex:1]).text @"Bar";

0 0