如何在不设置"userID"的情况下注销零 [英] How to logout without setting "userID" nil
问题描述
使用以下代码,我可以成功注销并在再次打开应用程序时显示登录屏幕.
With the following code I am able to logout successfully and show the login screen when the application is opened again.
我有一个用户个人资料页面,可在其中获取特定用户的所有数据.但是,登录后出现问题.我得到了所有用户的数据.然后,当我最小化该应用程序并再次打开它时,我的个人资料页面中的所有数据都丢失了.我认为这是因为我使用setObject
方法将用户ID设置为nil
.
I have a user profile page where I fetch all the data of a particular user. However, a problem occurs after logging in. I get all the user's data. Then when I minimise the application and open it again, all the data in my profile page is lost. I think this is because I am setting the user id to nil
with setObject
method.
有人对如何使这项工作有任何想法吗?
Does anyone have any idea about how to make this work?
- (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];
}
以下是我用于登出的代码:
Below is the code I am using to LOGOUT:
-(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{
}
推荐答案
当您调用[alert show]
时,您似乎在假设它停止在那里执行您的方法,并且仅当用户点击 OK <时才继续执行/em>.那样行不通.发生的结果是,您正在显示警报视图,方法继续,然后在点击任何按钮之前将userID
设置为nil
.然后,您在警报视图中点击一个按钮,有关您所点击内容的信息将被丢弃.
When you call [alert show]
, it seems like you're assuming that it stops executing your method there and only continues if the user taps OK. It doesn't work that way. What happens is that you're showing the alert view, your method continues, then sets your userID
to nil
immediately, before you tap any button. You then tap on a button in the alert view, and the information about what you tapped is thrown away.
如果您只想在某人点击 OK 时将其注销,则需要实现UIAlertViewDelegate
协议,并在委托方法中将userID
设置为nil
.点击右键.
If you want to log somebody out only if they tap OK, then you need to implement the UIAlertViewDelegate
protocol, and set your userID
to nil
in the delegate method if the right button is tapped.
请注意,不建议使用UIAlertView
,并且您应该在新代码中使用UIAlertController
.
Note that UIAlertView
is deprecated, and you should be using UIAlertController
in new code.
这篇关于如何在不设置"userID"的情况下注销零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!