微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 手动模式切换不起作用,View Controller不在窗口层次结构中?

我一直在搜索网页和Stack Overflow几个小时,我无法解决这个问题.这是希望你们都看到我的错误,因为我找不到.

我刚刚启动了一个简单的基于故事板的应用程序.初始ViewController是UITabBarController的一个实例,该模板具有两个虚拟ViewController.启动后,我需要检查设备是否登录到外部服务.如果不是,我会显示一个允许用户认证的模态ViewController,如果设备被认证,那么我将只显示FirstViewController.

以下步骤是创建项目后所做的一切:

>在故事板上创建AuthenticateViewController场景
>为AuthenticateViewController创建代码文件,并将其分配给相应的场景
>为UITabBarController子类创建代码文件,并将初始UITabBarController场景与该新子类相关联
>在故事板上从UITabBarController场景创建一个新的segue到AuthenticateViewController场景
>在UITabBarController子类中手动调用viewDidLoad中的segue

当我运行应用程序时,模式segue不会触发,UITabBarController的第一个ViewController被显示,并且我在XCode中得到以下输出

Warning: Attempt to present <AuthenticateViewController: 0x83c0c10> on <EPTabBarController: 0x83be600> whose view is not in the window hierarchy!

下面的相关代码,其实是迄今为止添加的唯一代码.如果屏幕截图或其他信息将会有用,请让我知道.在此先感谢您的帮助.

EPTabBarController,UITabBarController的子类:

#import "EPTabBarController.h"
#import "AuthenticateViewController.h"

@interface EPTabBarController ()

@end

@implementation EPTabBarController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self performSegueWithIdentifier:@"authenticationSegue" sender:self];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // dispose of any resources that can be recreated.
}

@end

解决方法

问题在里面
- (void)viewDidLoad
{
    [super viewDidLoad];    
    [self performSegueWithIdentifier:@"authenticationSegue" sender:self];
}

您正在尝试呈现另一个视图(AuthenticateViewController),而当前视图(EPTabBarController)尚未加载到窗口层次结构中.

所以首先让您的EPTabBarController加载到窗口层次结构中,然后显示AuthenticateViewController.

试试看吧

- (void)viewDidLoad 
{
    [super viewDidLoad];        
    [self performSelector:@selector(loadAuthenticateViewController) 
               withObject:nil 
               afterDelay:1.0];
}

-(void)loadAuthenticateViewController 
{
    [self performSegueWithIdentifier:@"authenticationSegue" sender:self];
}

原文地址:https://www.jb51.cc/iOS/329842.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐