IOS UISwitch 控件
转自:http://my.oschina.net/wolx/blog/396680
一 UISwitch 简介
UISwitch 的作用是给用户提供开关,在系统的设置界面很常见,控件也很简单。
二 UISwitch 创建
//创建
UISwitch *switch1 = [[UISwitch alloc]init];
CGSize viewSize = self.view.bounds.size;
switch1.frame = CGRectMake(viewSize.height*0.2, 150, 0, 0);
//使用 initWithFrame 方法初始化开关控件。
CGRect rect = CGRectMake(viewSize.height*0.2, 250, 0, 0);
UISwitch *switch2 = [[UISwitch alloc]initWithFrame:rect];
三设置选中状态
@property(nonatomic,getter=isOn) BOOL on;
on 属性用于控制开关状态,如果设置为YES 则表示开启,如果为NO 则表示关闭,可以通过isOn 方来判断
//1 设置开关状态
//1.1 setOn 方法
[switch1 setOn:YES];
//1.2 setOn:animated:方法。Animated 参数是布尔类型,若值为 YES 开关改变状态时会显 示动画
[switch2 setOn:YES animated:YES]
//2 判断状态
if ([switch1 isOn]){
NSLog(@"The switch is on.");
} else {
NSLog(@"The switch is off.");
}
四 添加监听
如果要在开关控件被打开或关闭时得到通知信息,可用利用 UISwitch 的addTarget:action:forControlEvents:方法加上开关的 target。
// 1. 添加监听
[switch1 addTarget:self action:@selector(switchIsChanged:) forControlEvents:UIControlEventValueChanged];
// 2.事件发生后执行的方法
/**
* switchIsChanged 方法,用于监听UISwitch控件的值改变
*
* @param swith swith 控件
*/
-(void)switchIsChanged:(UISwitch *)swith
{
if ([swith isOn]){
NSLog(@"The switch is on.");
} else {
NSLog(@"The switch is off.");
}
}
五 测试代码
5.1 代码
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1.创建
UISwitch *switch1 = [[UISwitch alloc]init];
CGSize viewSize = self.view.bounds.size;
switch1.frame = CGRectMake(viewSize.height*0.2, 150, 0, 0);
//1.1使用 initWithFrame 方法初始化开关控件。
CGRect rect = CGRectMake(viewSize.height*0.2, 250, 0, 0);
UISwitch *switch2 = [[UISwitch alloc]initWithFrame:rect];
//2 设置默认选中
//@property(nonatomic,getter=isOn) BOOL on;
[switch1 setOn:YES];
//2.1 setOn:animated:方法。Animated 参数是布尔类型,若值为 YES 开关改变状态时会显 示动画
[switch2 setOn:YES animated:YES];
//3.判断是否选中
if ([switch1 isOn]){
NSLog(@"The switch is on.");
} else {
NSLog(@"The switch is off.");
}
//4若希望在开关控件被打开或关闭时得到通知信息,就必须在你的类中,利用 UISwitch 的addTarget:action:forControlEvents:方法加上开关的 target。如下代码:
[switch1 addTarget:self action:@selector(switchIsChanged:) forControlEvents:UIControlEventValueChanged];
//添加到UIView
[self.view addSubview:switch1];
[self.view addSubview:switch2];
}
/**
* switchIsChanged 方法,用于监听UISwitch控件的值改变
*
* @param swith swith 控件
*/
-(void)switchIsChanged:(UISwitch *)swith
{
if ([swith isOn]){
NSLog(@"The switch is on.");
} else {
NSLog(@"The switch is off.");
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
5.2 执行结果
2015-04-07 00:00:59.435 2UISwitch[1220:29996] The switch is on.
2015-04-07 00:01:06.134 2UISwitch[1220:29996] The switch is off.
2015-04-07 00:01:08.424 2UISwitch[1220:29996] The switch is on.
2015-04-07 00:11:57.685 2UISwitch[1220:29996] The switch is off.
2015-04-07 00:12:03.681 2UISwitch[1220:29996] The switch is on.
2015-04-07 00:12:04.219 2UISwitch[1220:29996] The switch is off.
2015-04-07 00:12:04.965 2UISwitch[1220:29996] The switch is on.