當(dāng)前位置:商國互聯(lián)首頁 供應(yīng)信息 IT設(shè)備、數(shù)碼產(chǎn)品、軟件 其他專用軟件 實驗室專用軟件

IOS UISwitch 控件
一. UISwitch 簡介
UISwitch 的作用是給用戶提供開關(guān),在系統(tǒng)的設(shè)置界面很常見,控件也很簡單。
二. UISwitch 創(chuàng)建
?
1
2
3
4
5
6
7
8
9
//創(chuàng)建
UISwitch *switch1 = [[UISwitch alloc]init];
CGSize viewSize = self.view.bounds.size;
switch1.frame = CGRectMake(viewSize.height*0.2, 150, 0, 0);
//使用 initWithFrame 方法初始化開關(guān)控件。
CGRect rect = CGRectMake(viewSize.height*0.2, 250, 0, 0);
UISwitch *switch2 = [[UISwitch alloc]initWithFrame:rect];
三. 設(shè)置選中狀態(tài)
?
1
@property(nonatomic,getter=isOn) BOOL on;
on 屬性用于控制開關(guān)狀態(tài),如果設(shè)置為YES 則表示開啟,如果為NO 則表示關(guān)閉,可以通過isOn 方來判斷
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//1 設(shè)置開關(guān)狀態(tài)
//1.1 setOn 方法
[switch1 setOn:YES];
//1.2 setOn:animated:方法。Animated 參數(shù)是布爾類型,若值為 YES 開關(guān)改變狀態(tài)時會顯 示動畫
[switch2 setOn:YES animated:YES]
//2 判斷狀態(tài)
if ([switch1 isOn]){
NSLog(@"The switch is on.");
} else {
NSLog(@"The switch is off.");
}
四. 添加監(jiān)聽
如果要在開關(guān)控件被打開或關(guān)閉時得到通知信息,可用利用 UISwitch 的addTarget:action:forControlEvents:方法加上開關(guān)的 target。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 1. 添加監(jiān)聽
[switch1 addTarget:self action:@selector(switchIsChanged:) forControlEvents:UIControlEventValueChanged];
// 2.事件發(fā)生后執(zhí)行的方法
/**
* switchIsChanged 方法,用于監(jiān)聽UISwitch控件的值改變
*
* @param swith swith 控件
*/
-(void)switchIsChanged:(UISwitch *)swith
{
if ([swith isOn]){
NSLog(@"The switch is on.");
} else {
NSLog(@"The switch is off.");
}
}
五. 測試代碼
5.1 代碼
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1.創(chuàng)建
UISwitch *switch1 = [[UISwitch alloc]init];
CGSize viewSize = self.view.bounds.size;
switch1.frame = CGRectMake(viewSize.height*0.2, 150, 0, 0);
//1.1使用 initWithFrame 方法初始化開關(guān)控件。
CGRect rect = CGRectMake(viewSize.height*0.2, 250, 0, 0);
UISwitch *switch2 = [[UISwitch alloc]initWithFrame:rect];
//2 設(shè)置默認(rèn)選中
//@property(nonatomic,getter=isOn) BOOL on;
[switch1 setOn:YES];
//2.1 setOn:animated:方法。Animated 參數(shù)是布爾類型,若值為 YES 開關(guān)改變狀態(tài)時會顯 示動畫
[switch2 setOn:YES animated:YES];
//3.判斷是否選中
if ([switch1 isOn]){
NSLog(@"The switch is on.");
} else {
NSLog(@"The switch is off.");
}
//4若希望在開關(guān)控件被打開或關(guān)閉時得到通知信息,就必須在你的類中,利用 UISwitch 的addTarget:action:forControlEvents:方法加上開關(guān)的 target。如下代碼:
[switch1 addTarget:self action:@selector(switchIsChanged:) forControlEvents:UIControlEventValueChanged];
//添加到UIView
[self.view addSubview:switch1];
[self.view addSubview:switch2];
}
/**
* switchIsChanged 方法,用于監(jiān)聽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 執(zhí)行結(jié)果
?
1
2
3
4
5
6
7
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.
IOS UISwitch 控件
*您的姓名:
*聯(lián)系手機(jī):
固話電話:
E-mail:
所在單位:
需求數(shù)量:
*咨詢內(nèi)容: