突然觉得好久没有更新博客了,今天就想把自己的项目中的一些功能和常用的模块写出来给大家参考一下。。。
这是我的二个项目中的不同的设置界面,第一个设置的那个按钮是 用的开关switch ,当然这个就容易一些了,你只用控制他的 no 和yes 。里面的逻辑就是,你需要在你的引导页那里设置他的初始化,因为你刚进来肯定要给他设置全部打开的,因为这是程序中的一个惯性吧,然后,如果你有引导页的话,你就需要在引导页中设置他的初始化,给他设置 了初始化,后续就保存他的状态到本地就可以,每次进来只需要读取他的状态来设置就可以,
1.在引导页中初始化他的状态代码(这是有引导页的情况下写到引导页里面的,进行初始化)
//程序刚运行的时候给他设置为0
[[NSUserDefaults standardUserDefaults]setInteger:0 forKey:@"countMessageNumber"];
[[NSUserDefaults standardUserDefaults] synchronize];
//新消息,声音,震动
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isNewMessage"];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isVoice"];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isVibrate"];
//这是开关控件的设置为YES
2 .在设置页面进行控制他的开关设置,因为你的接受新消息通知打开,下面的全部要打开,如果关闭下面的全部要关闭,下面是代码(看不懂的问我)
1 #pragma mark 切换开关转化事件 (这是第一个设置的代码,这个是开关按钮写的)
2 -(void)statuesTableViewCell:(MessageSettingTableViewCell *)cell reloadTableView:(UISwitch *)sw
3 {
4 NSLog(@"section:%li,switch:%i",(long)sw.tag, sw.isOn);
5 switch (cell.tag) {
6 case 1000:
7 {
8 if (sw.isOn)// 控制开关--------如果开了,全开
9 {
10
11 isShowCell=YES;
12 IsShowFirst = YES;
13 isShowSecond = YES;
//设置cell的收缩控制
15 [WDLUserDefaults setBool:isShowCell forKey:@"isShowCell"];
16 [WDLUserDefaults setBool:IsShowFirst forKey:@"IsShowFirst"];
17 [WDLUserDefaults setBool:isShowSecond forKey:@"isShowSecond"];
18
19 isNewMessage = YES;
20 isVibrate = YES;
21 isVoice = YES;
22
23 //新消息,声音,震动 实时的更新消息的状态
24 [WDLUserDefaults setBool:isNewMessage forKey:@"isNewMessage"];
25 [WDLUserDefaults setBool:isVoice forKey:@"isVoice"];
26 [WDLUserDefaults setBool:isVibrate forKey:@"isVibrate"];
27
28 [[NSUserDefaults standardUserDefaults] synchronize];
29 [_m_table reloadData];
30
31 }
32
33 else{
34
35 isShowCell=NO;
36 IsShowFirst = NO;
37 isShowSecond = NO;
38
39 [WDLUserDefaults setBool:isShowCell forKey:@"isShowCell"];
40 [WDLUserDefaults setBool:IsShowFirst forKey:@"IsShowFirst"];
41 [WDLUserDefaults setBool:isShowSecond forKey:@"isShowSecond"];
42
43 isNewMessage = NO;
44 isVibrate = NO;
45 isVoice = NO;
46
47 //新消息,声音,震动 实时的更新消息的状态
48 [WDLUserDefaults setBool:isNewMessage forKey:@"isNewMessage"];
49 [WDLUserDefaults setBool:isVoice forKey:@"isVoice"];
50 [WDLUserDefaults setBool:isVibrate forKey:@"isVibrate"];
51 [[NSUserDefaults standardUserDefaults] synchronize];
52
53 [_m_table reloadData];
54
55 }
56 }
57 break;
58 case 1001:
59 {
60
61 // 判断是否打开
62 if (sw.isOn)// 控制开关--------如果开了,全开
63 {
64
65 isVoice = YES;
66
67 //新消息,声音,震动
68 [WDLUserDefaults setBool:isVoice forKey:@"isVoice"];
69
70 [[NSUserDefaults standardUserDefaults] synchronize];
71 // [_m_table reloadData];
72
73 }else{
74
75 isVoice = NO;
76
77 //新消息,声音,震动
78
79 [WDLUserDefaults setBool:isVoice forKey:@"isVoice"];
80
81 [[NSUserDefaults standardUserDefaults] synchronize];
82 // [_m_table reloadData];
83
84 }
85
86 }
87 break;
88
89 case 1002:
90 {
91
92 // 判断是否打开
93 if (sw.isOn)// 控制开关--------如果开了,全开
94 {
95
96 isVibrate = YES;
97
98 //新消息,声音,震动
99 [WDLUserDefaults setBool:isVibrate forKey:@"isVibrate"];
100 [[NSUserDefaults standardUserDefaults] synchronize];
101 // [_m_table reloadData];
102 }
103
104 else{
105
106 isVibrate = NO;
107
108 //新消息,声音,震动
109 [WDLUserDefaults setBool:isVibrate forKey:@"isVibrate"];
110
111 [[NSUserDefaults standardUserDefaults] synchronize];
112 // [_m_table reloadData];
113 }
114
115 }
116 break;
117
118 case 2000:
119 {
120
121 }
122 break;
123
124 default:
125 break;
126 }
127
128 // [[NSNotificationCenter defaultCenter] postNotificationName:@"MessageisVoice" object:nil];
129 }
3 .这样就在每个地方进行对他对key进行调用。来控制他的语音和振动的设置
4 .下面说第二个设置的控制
4.1 这个按钮是一个真的 按钮 UIButton 。点击进行图片的切换,这个有点小坑。不过原理都一样
1 - (void)viewDidLoad {
2 [super viewDidLoad];
3
4 NSUserDefaults *messageDeful=[NSUserDefaults standardUserDefaults];
5
6 //接受新消息通知
7 NSString *isMessage_notf = [messageDeful objectForKey:@"isMessage_notf"];
8 _str_Message=@"1";
9 NSLog(@"isMessage_notf %@",isMessage_notf);
10
11 if (isMessage_notf == nil) {
12 [messageDeful setObject:_str_Message forKey:@"isMessage_notf"];
13 [messageDeful synchronize ];
14 }
15
16 //设置声音
17 NSString *isVoice_Set = [messageDeful objectForKey:@"isVoice_Set"];
18 _str_Voice=@"1";
19 NSLog(@"isVoice_Set %@",isVoice_Set);
20 if (isVoice_Set == nil) {
21 [messageDeful setObject:_str_Voice forKey:@"isVoice_Set"];
22 [messageDeful synchronize ];
23
24 }
25
26 // 设置振动
27 NSString *isVibration = [messageDeful objectForKey:@"isVibration_Set"];
28 _str_isVibration=@"1";
29 NSLog(@"isVibration %@",isVibration);
30
31 if (isVibration == nil) {
32 [messageDeful setObject:_str_isVibration forKey:@"isVibration_Set"];
33 [messageDeful synchronize ];
34 }
35 //设置扬声器打开
36 NSString *isSpeaker = [messageDeful objectForKey:@"isSpeaker_Set"];
37 _str_isSpeaker=@"1";
38 NSLog(@"isSpeaker %@",isSpeaker);
39
40 if (isSpeaker == nil) {
41 [messageDeful setObject:_str_isSpeaker forKey:@"isSpeaker_Set"];
42 [messageDeful synchronize ];
43 }
44
45 _huud = [[MBProgressHUD alloc]init];
46 }
47
48 -(void)viewWillAppear:(BOOL)animated
49 {
50 [super viewWillAppear:animated];
51
52 NSString *is_Message=[userdefault objectForKey:@"isMessage_notf"];
53 if ([is_Message isEqualToString:@"1"]) {
54
55 [_messageaccept setImage:[UIImage imageNamed:@"switch_on@2X.png"] forState:UIControlStateNormal];
56 }else if([is_Message isEqualToString:@"0"])
57 {
58 [_messageaccept setImage:[UIImage imageNamed:@"switch_off@2X.png"] forState:UIControlStateNormal];
59 }
60
61 NSString *isVoice=[userdefault objectForKey:@"isVoice_Set"];
62 if ([isVoice isEqualToString:@"1"]) {
63
64 [_voice setImage:[UIImage imageNamed:@"switch_on@2X.png"] forState:UIControlStateNormal];
65
66 }else if([isVoice isEqualToString:@"0"])
67 {
68 [_voice setImage:[UIImage imageNamed:@"switch_off@2X.png"] forState:UIControlStateNormal];
69 }
70
71 NSString *isVibration=[userdefault objectForKey:@"isVibration_Set"];
72 if ([isVibration isEqualToString:@"1"]) {
73
74 [_vibration setImage:[UIImage imageNamed:@"switch_on@2X.png"] forState:UIControlStateNormal];
75 }else if([isVibration isEqualToString:@"0"])
76 {
77 [_vibration setImage:[UIImage imageNamed:@"switch_off@2X.png"] forState:UIControlStateNormal];
78 }
79
80 NSString *isSperk=[userdefault objectForKey:@"isSpeaker_Set"];
81 if ([isSperk isEqualToString:@"1"]) {
82
83 [_speaker setImage:[UIImage imageNamed:@"switch_on@2X.png"] forState:UIControlStateNormal];
84 }else if([isSperk isEqualToString:@"0"])
85 {
86 [_speaker setImage:[UIImage imageNamed:@"switch_off@2X.png"] forState:UIControlStateNormal];
87 }
88 }
View Code
这是在ViewDidLoud里面设置他的初始化,因为我这个项目中没有设置引导页,所以在ViewDidLoad里面进行了初始化,然后在viewWillAppear里面设置了他的点击更换图片的一些操作。然后每个地方用的话,只需要设置一些他的属性或者在.m里面设置他的变量,然后获得他的Key值,进行获取他的状态,然后根据他的状态来设置他的声音还是振动了。。