iphone - 在加载另一个 ViewController 时呈现模态视图 Controller

标签 iphone ios uikit

在用户可以使用我的应用程序之前,他必须先登录。他登录后,数据库就建立好了,因为我需要服务器的信息来建立它。

因此,我的根 ViewController 是 LoginViewController,它在成功登录时以模态方式呈现实际应用程序(navigationController 堆栈)。

如果用户已经在应用程序启动时登录(我将凭据存储在 NSUserDefaults 中),则 LoginViewController 应该立即显示该应用程序。因此我重写了方法:

- (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]; 已执行,但 Controller 未显示。如果我使用上面的行,它确实有效。

我必须将代码放在哪里才能使其工作?

最佳答案

发送 viewDidAppear 时, View 堆栈可能未完全创建。因此,您应该使用 perfomSelector:withDelay 在运行循环中对调用进行排队。通过这种方式,您可以确保在代码运行时构建 View 堆栈。

干杯!

关于iphone - 在加载另一个 ViewController 时呈现模态视图 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6405507/

相关文章:

iphone - 无法在我的 tableView 中使用我的 NSString 对象

iphone - 将图层阴影裁剪为宽度而不是高度?

iphone - 如何在编写漂亮代码的同时避免扩展 NSArray 和在 Java 中思考

ios - 如何使两个 Collection View 出现在同一个 View Controller 上?

ios - Cocos 2D - 在平铺 map 上移动 Sprite

ios - keyboardWillChangeFrame 没有按预期工作

iphone - 编辑 TableView 时自定义 TableCell 中没有缩进

ios - 钛iOS : Anything wrong with a global navgroup?

ios - 步长不均匀的 UISlider。

ios - (Swift 3) - 如何在自定义 UIView 中包含动画