1

我想打开一个登录屏幕,我已经尝试过编程方式和 segue。我见过类似的问题,但它并没有为我解决。

这是我的两个版本的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if (![[API sharedInstance] isAuthorized]) {
        NSLog(@"I should Open login screen");
        [self performSegueWithIdentifier:@"ShowLogin" sender:nil];
    }
}

或者

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if (![[API sharedInstance] isAuthorized]) {
        NSLog(@"I should Open login screen");
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    LoginScreen *vc = [sb instantiateViewControllerWithIdentifier:@"LoginScreen"];
    vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:vc animated:YES completion:NULL];
    }
}

segue 是模态风格。

在这两种情况下,都会打印 NSLog,然后我看到一个警告:

Warning: Attempt to present <LoginScreen: 0x1e5bd010> on <PhotoScreen: 0x1e5b82e0> whose view is not in the window hierarchy!

并且新视图没有打开。

有什么提示吗?

4

1 回答 1

2

不要在 viewDidLoad 中执行此操作,您的视图尚未出现在屏幕上,这就是您收到该错误的原因。您应该在 viewDidAppear 中执行此操作(如果您不想看到此代码所在的控制器的视图,则没有动画)。

于 2013-08-15T15:29:04.433 回答