【发布时间】:2016-10-06 02:59:38
【问题描述】:
使用以下代码,我可以成功注销并在再次打开应用程序时显示登录屏幕。
我有一个用户个人资料页面,我可以在其中获取特定用户的所有数据。但是,登录后出现问题。我得到了所有用户的数据。然后,当我最小化应用程序并再次打开它时,我的个人资料页面中的所有数据都丢失了。我认为这是因为我使用setObject 方法将用户ID 设置为nil。
有人知道如何进行这项工作吗?
- (IBAction)logoutButtonTapped:(id)sender {
UIAlertView* alert = [[UIAlertView alloc]
initWithTitle:@"Confirmation"
message:@"Do you want to Logout?"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
[alert show];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:nil forKey:@"userID"];
[prefs synchronize];
}
以下是我用于注销的代码:
-(void)nextScreen: (NSTimer *) timer
{
NSLog(@"TEST");
NSString * storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
MenuScreenViewController *viewcontroller = [storyboard instantiateViewControllerWithIdentifier:@"loginScreenViewController"];
[self.navigationController pushViewController:viewcontroller animated:YES];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex
{
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:2
target:self
selector:@selector(nextScreen:)
userInfo:nil
repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
[timer fire];
if (buttonIndex != 0) // 0 == the cancel button
{
//home button press programmatically
UIApplication *app = [UIApplication sharedApplication];
[app performSelector:@selector(suspend)];
//wait 2 seconds while app is going background
[NSThread sleepForTimeInterval:2.0];
//exit app when app is in background
NSLog(@"TIMER CALLED");
exit(0);
}else{
}
【问题讨论】:
-
有人知道我应该在 else 方法中使用什么来在同一视图上或关闭警报视图
标签: ios objective-c login logout