我有UITableView
,其中包含UISwitch
按钮。我希望当我运行我的应用程序时,UISwitch
按钮的默认值应为ON
。如果我将开关按钮从打开切换为关闭,则plist中的值应更改为OFF
。如果我退出应用程序然后再次运行我的应用程序,该值应再次默认为plist中的ON
。
我尝试过使用NSUserDefaults
,但是当我将值从ON
更改为OFF
时,NSUserDefaults
中的值也会发生变化。但是,当再次运行应用程序时,默认值不会设置为ON
。
- (void)viewDidLoad {
[super viewDidLoad];
settings = [[NSMutableArray alloc]initWithObjects:@"Clock",@"Time Format",@"Weather",@"Degrees",nil];
switchControl = [[UISwitch alloc]initWithFrame:CGRectMake(205, 110, 20, 15) ];
[self.switchControl addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:switchControl];
NSString *viewdid = @"ON";
userdefaults = [NSUserDefaults standardUserDefaults];
[userdefaults setObject:viewdid forKey:@"stateOfSwitch"];
}
- (void)viewWillAppear:(BOOL)animated {
NSString *_value= [[NSUserDefaults standardUserDefaults] stringForKey:@"stateOfSwitch"];
if([_value compare:@"ON"] == NSOrderedSame){
[switchControl setOn:YES animated:YES];
}
else {
[switchControl setOn:NO animated:YES];
}
[super viewWillAppear:animated];
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 4;
}
-(void)switchChanged:(id)sender
{
app= (StopSnoozeAppDelegate*)[[UIApplication sharedApplication]delegate];
NSString *value = @"ON";
userdefaults = [NSUserDefaults standardUserDefaults];
if(!switchControl.on){
value = @"OFF";
[userdefaults setObject:value forKey:@"stateOfSwitch"];
}
[userdefaults setObject:value forKey:@"stateOfSwitch"];
[userdefaults synchronize];
}
//this is my second class where i am accessing my userdefaults key and depending on that hiding the views
- (void) viewWillAppear:(BOOL)animated
{
NSString *_value= [[NSUserDefaults standardUserDefaults] stringForKey:@"stateOfSwitch"];
if([_value compare:@"ON"] == NSOrderedSame){
newview.hidden = NO;
newnewview.hidden =NO;
firstview.hidden = NO;
secondview.hidden = NO;
thirdview.hidden = NO;
}
else if([_value compare:@"OFF"] == NSOrderedSame) {
newview.hidden = YES;
newnewview.hidden= YES;
firstview.hidden = YES;
secondview.hidden = YES;
thirdview.hidden = YES;
}
}
答案 0 :(得分:1)
在appDelegate中完成加载写下面的语句
[[NSUserDefaults standardUserDefaults] setObject:@"OFF" forKey:@"stateOfSwitch"];
[[NSUserDefaults standardUserDefaults] synchronize];
答案 1 :(得分:0)
您可能没有将新值转储到plist?
将新值设置为[[NSUserDefaults standardUserDefaults] synchronize];
后,尝试致电NSUserDefaults
。
答案 2 :(得分:0)
要么
// set when launching the app
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[[NSUserDefaults standardUserDefaults] setObject:@"OFF" forKey:@"stateOfSwitch"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
或
// set when terminating
- (void)applicationWillTerminate:(UIApplication *)application
[[NSUserDefaults standardUserDefaults] setObject:@"OFF" forKey:@"stateOfSwitch"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
此外,您应该使用isEqualToString:
测试您的NSString,因为compare:
可能导致误报(例如,如果其中一个字符串为零)。
答案 3 :(得分:0)
当NSUserDefaults完全能够直接存储BOOL时,你不应该存储一个有可能解析bug的字符串。
只需将其存储起来:
#define StateOfSwitch @"StateOfSwitch"
-(void)switchChanged:(id)sender
{
[[NSUserDefaults standardUserDefaults] setBool:switchControl.on forKey:StateOfSwitch];
[[NSUserDefaults standardUserDefaults] synchronize];
}
然后再加载这样的值:
switchControl.on = [[NSUserDefaults standardUserDefaults] boolForKey:StateOfSwitch];
(注意,永远不要直接使用字符串键是个好主意,最好#define它们以防止拼写错误。另外,你可以通过命令+点击跳转到定义,自动完成等等。)