1 // Copyright 用户首选项的创建和保存 2 // 1.保存方式:创建一个纯文本文件,或是使用更规范的格式(如XML)来保存 3 // 2.在数据库中保存首选项 4 // 3.使用USUserDefault 5 // 6 // USUserInfo常见方法: 7 // 1.standardUserDefaults 创建共享默认对象方法 8 // 2.objectForKey 返回键的实例方法。保存在许多变体,可以返回特定对象类型,比如字符串、布尔值等 9 // 3.setobjectForKey 设置对象值的实例方法。保存在许多变体,可以返回特定对象类型,比如字符串、布尔值等 10 // 4.objectForKey 保存共享类对象发生的所有更改的类方法 11 12 13 #import "TheadViewController.h" 14 15 @interface TheadViewController () 16 17 @end 18 19 @implementation TheadViewController 20 21 - (id)initWithStyle:(UITableViewStyle)style 22 { 23 self = [super initWithStyle:style]; 24 if (self) { 25 NSUserDefaults *myDefaults=[NSUserDefaults standardUserDefaults]; 26 27 //提取并设置声音值 28 NSMutableArray *array1=[NSMutableDictionary 29 dictionaryWithObjectsAndKeys:@"Sounds",@"titleValue", 30 @"switch",@"accessoryValue", 31 [NSNumber numberWithBool:[myDefaults boolForKey:@"soundsValue"]],@"prefValue", 32 @"setSounds",@"targetValue",nil]; 33 34 //提取并设置音乐值 35 NSMutableArray *array2=[NSMutableDictionary 36 dictionaryWithObjectsAndKeys:@"Music",@"titleValue", 37 @"switch",@"accessoryValue", 38 [NSNumber numberWithBool:[myDefaults boolForKey:@"MusicValue"]],@"prefValue", //2 39 @"setMusic",@"targetValue",nil]; 40 41 settingList=[NSArray arrayWithObjects:array1,array2,nil]; 42 [settingList retain]; 43 switchList=[NSMutableArray arrayWithCapacity:settingList.count]; 44 45 for (int i=0; i<[settingList count]; i++) { 46 if ([[[settingList objectAtIndex:i]objectForKey:@"accessoryValue"] compare:@"switch"]==NSOrderedSame) { 47 //实例一个UISwitch并注册事件 48 UISwitch *myswitch=[[[UISwitch alloc]initWithFrame:CGRectZero]autorelease]; 49 myswitch.on=[[[settingList objectAtIndex:i]objectForKey:@"prefValue"]boolValue]; 50 [myswitch addTarget:self action:NSSelectorFromString([[settingList objectAtIndex:i]objectForKey:@"targetValue"]) forControlEvents:UIControlEventValueChanged]; 51 52 [switchList insertObject:myswitch atIndex:i]; 53 }else{ 54 [switchList insertObject:@"" atIndex:i]; 55 } 56 } 57 [switchList retain]; 58 59 //将表格下移 60 CGPoint tableCenter=self.view.center; 61 self.view.center=CGPointMake(tableCenter.x, tableCenter.y+22); 62 self.tabBarItem.badgeValue=[NSString stringWithFormat:@"%d",[settingList count]]; 63 } 64 return self; 65 } 66 67 - (void)viewDidLoad 68 { 69 [super viewDidLoad]; 70 71 } 72 73 #pragma mark --执行操作 74 75 -(void)setMusic{ 76 NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults]; //1 77 UISwitch *musicSwitch=[switchList objectAtIndex:1]; 78 [defaults setBool:musicSwitch.on forKey:@"MusicValue"]; //3 79 [NSUserDefaults resetStandardUserDefaults]; //4 80 } 81 82 -(void)setSounds{ 83 NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults]; 84 UISwitch *soundSwitch=[switchList objectAtIndex:0]; 85 [defaults setBool:soundSwitch.on forKey:@"soundsValue"]; 86 [NSUserDefaults resetStandardUserDefaults]; 87 88 } 89 90 - (void)didReceiveMemoryWarning 91 { 92 [super didReceiveMemoryWarning]; 93 // Dispose of any resources that can be recreated. 94 } 95 96 #pragma mark - Table view data source 97 98 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 99 { 100 return 1; 101 } 102 103 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 104 { 105 return settingList.count; 106 } 107 108 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 109 { 110 static NSString *CellIdentifier = @"Cell"; 111 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 112 if (cell == nil) { 113 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 114 } 115 cell.textLabel.text=[[settingList objectAtIndex:indexPath.row]objectForKey:@"titleValue"]; 116 117 //将视图放入到数组 118 if ([switchList objectAtIndex:indexPath.row]) { 119 cell.accessoryView=[switchList objectAtIndex:indexPath.row]; 120 } 121 122 return cell; 123 } 124 125 126 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ 127 return @"Audio Preferencess"; 128 } 129 130 #pragma mark - Table view delegate 131 132 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 133 { 134 } 135 136 @end