加载另一个ViewController时呈现一个模式视图控制器 [英] Presenting a modal view controller when loading another ViewController
问题描述
在用户可以使用我的应用程序之前,他必须登录.他登录后,便建立了数据库,因为我需要来自服务器的信息才能建立数据库.
Before the user can use my application he has to login. After he logged in, the database is built because I need information from the server to build it.
因此,我的根ViewController是LoginViewController,它在成功登录后以模态形式呈现实际的应用程序(navigationController堆栈).
Therefore my root ViewController is the LoginViewController which presents the actual application (a navigationController stack) modally on successful login.
如果用户已经在应用程序启动时登录(我正在使用NSUserDefaults存储凭据),LoginViewController应该立即显示该应用程序.因此,我重写了该方法:
If the user is already logged in on application launch (I am storing the credentials with NSUserDefaults) the LoginViewController should present the application immediately. Therefore I overwrote the method:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSInteger userId = [[NSUserDefaults standardUserDefaults] integerForKey:@"selfUser"];
if (userId != 0) {
//[self performSelector:@selector(presentMainViewController) withObject:nil afterDelay:2];
[self presentMainViewController];
}
}
- (void)presentMainViewController {
mainViewController = [[MainViewController alloc] init];
mainViewController.managedObjectContext = managedObjectContext;
navigationController = [[UINavigationController alloc] initWithRootViewController:mainViewController];
navigationController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:navigationController animated:NO];
}
已执行 [self presentMainViewController];
,但控制器未显示.如果我使用上面的行,它确实起作用.
The [self presentMainViewController];
is executed but the controller does not show up. If I use the line above it does work.
我必须在哪里放置代码以使其起作用?
Where do I have to put the code to make it work?
推荐答案
发送 viewDidAppear
时,视图堆栈可能未完全创建.因此,您应该使用 perfomSelector:withDelay
在运行循环中将调用排队.这样,您可以确保在代码运行时构建视图堆栈.
The view stack might not be completely created when viewDidAppear
is send. So you should use the perfomSelector:withDelay
to queue the call on the run loop. In this way you can ensure that the view stack is build when your code runs.
干杯!
这篇关于加载另一个ViewController时呈现一个模式视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!