我刚刚启动了一个简单的基于故事板的应用程序.初始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];
}