当我点击登出按钮视图进入登录页面并再次打开我的主页..这是我的问题..
这是我的代码 -
此代码用于检查NSUserDefault
-(void)viewWillAppear:(BOOL)animated{
self.navigationController.navigationBar.hidden = YES;
// cheq the value in NSUserdeafault
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"login"] boolValue]) {
HomePage *mainView = [[HomePage alloc]initWithNibName:@"HomePage" bundle:nil];
[self.navigationController pushViewController:mainView animated:YES];
}
}
这个用于退出按钮
- (IBAction)logout_btn:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:@"0" forKey:@"logout"];
[defaults synchronize];
LoginPage *log = [[LoginPage alloc] init];
MyNavigationController *navController = [[MyNavigationController alloc] initWithRootViewController:log];
navController.navigationBar.translucent=NO;
[menuController setRootController:navController animated:NO];
}
请给我解决方案
答案 0 :(得分:1)
将Bool数据类型用于“登录”键。
登录时使用:
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"login"];
[[NSUserDefaults standardUserDefaults] synchronize];
点击退出按钮时设置:
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"login"];
[[NSUserDefaults standardUserDefaults] synchronize];
答案 1 :(得分:0)
您正在为UserDefault设置一个字符串,这将使您的检查成立。
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:@"0" forKey:@"logout"];
它应该是这样的:
[[NSUserDefaults standardUserDefaults] setValue:@NO forKey:@"logout"];
或
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"logout:]; // explicitly indicating that you are setting a BOOL
然后在你的支票中你可以使用
[[[NSUserDefaults standardUserDefaults] boolForKey:@"logout"]; // you won't need to specify boolValue anymore unless you use the first option (setValue:@NO)