我正在使用 Storyboards 开发一个应用程序。此应用程序要求用户登录。
故事板初始时有一个登录视图。当登录正常时,它对TabViewController执行 segue 。
在对 API 的任何调用中,我都会检查服务器是否返回 401(未授权)。如果发生这种情况,则将布尔值设置为 false(布尔 isLogged)。AppDelegate 观察这个布尔值。如果值更改为 false,我想将用户返回到登录屏幕(请记住,故事板上的初始视图)。
这是一些代码:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"isLogged"]) {
BOOL logged = [[change objectForKey:NSKeyValueChangeNewKey] boolValue];
if (logged) {
NSLog(@"Logged in succesfully!");
} else {
NSLog(@"Logout performed");
[self.window makeKeyAndVisible];
[self.window layoutSubviews];
}
}
这工作正常,但是当我尝试再次登录LoginOK segue 时没有执行。
我尝试了许多其他选项,例如:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *loginController = [storyboard instantiateViewControllerWithIdentifier:@"Login"];
[source presentModalViewController:loginController animated:YES];
但问题是我不知道哪个 View Controller 执行了注销。源必须是应用程序中显示的当前视图控制器,不是吗?