10

我已经在网络和 Stack Overflow 上搜索了几个小时,但我无法解决这个问题。这里希望你们都看到我的错误,因为我找不到它。

我刚开始有一个简单的基于故事板的应用程序。初始 ViewController 是 UITabBarController 的一个实例,其中包含模板中的两个虚拟 ViewController。启动时,我需要检查设备是否登录到外部服务。如果不是,我将显示一个允许用户进行身份验证的模式 ViewController,如果设备通过了身份验证,那么我将只显示 FirstViewController。

以下步骤是我自创建项目以来所做的一切:

  1. 在 Storyboard 上创建 AuthenticateViewController 场景
  2. 为AuthenticateViewController创建代码文件,并分配给对应的场景
  3. 为 UITabBarController 子类创建代码文件,并将初始 UITabBarController 场景关联到该新子类
  4. 在情节提要上创建一个从 UITabBarController 场景到 AuthenticateViewController 场景的新转场
  5. viewDidLoad从UITabBarController 子类中手动调用 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
4

3 回答 3

14

问题在里面

- (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];
}
于 2013-03-25T04:56:16.293 回答
3

在 viewDidAppear 中简单地调用您的代码怎么样?

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

但是,我仍然对这些解决方案不满意,因为原始视图仍然显示了几分之一秒。

关于如何在不首先显示原始视图的情况下显示新模式的任何想法?

于 2014-06-10T07:51:28.210 回答
2

当您真的想要控制时,请使用以下内容:

@implementation UeInitialisationViewController
BOOL didLoad;
BOOL wantPush;

- (id)init {
    self = [super init];
    if (self) { didLoad = NO; wantPush = NO; }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    didLoad = YES;
    if (wantPush == YES)
        [self performSelector:@selector(pushToMainView) 
                   withObject:nil afterDelay:0.0001];
}

- (void)pushToMainView {
    if (didLoad == YES)
        [self performSegueWithIdentifier:@"pushToMainView" sender:self];
    else
        wantPush = YES;
}
@end

pushToMainView这将在您发送消息时触发 Segue ,[controller pushToMainView]但会阻止,直到加载视图。

于 2013-10-06T10:35:05.893 回答