我有一个带UISwitch按钮的UITableView。我希望当我运行我的应用程序时,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;
}
}发布于 2011-10-17 15:16:01
在appDelegate中完成了write below语句的加载
[[NSUserDefaults standardUserDefaults] setObject:@"OFF" forKey:@"stateOfSwitch"];
[[NSUserDefaults standardUserDefaults] synchronize];发布于 2011-10-17 14:20:24
可能是你没有将新的值转储到plist?
在将新值设置为NSUserDefaults后尝试调用[[NSUserDefaults standardUserDefaults] synchronize];。
发布于 2011-10-17 15:19:30
我也不知道
// 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:可能会导致误报(例如,如果其中一个字符串为空)。
https://stackoverflow.com/questions/7789927
复制相似问题