2013-03-25 40 views
10

我一直在搜索网络和堆栈溢出几个小时,我无法解决这个问题。希望你们都能看到我的错误,因为我找不到它。手动模态segue不起作用,View Controller不在窗口层次结构中?

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

下面的步骤是一切,因为在创建项目我做:

  1. 在故事板
  2. 创建AuthenticateViewController场景创建AuthenticateViewController代码文件,并将它们分配到相应的场景
  3. 创建用于UITabBarController子类的代码文件,并将初始UITabBarController场景关联到该新子类
  4. 在故事板上从UITabBarController场景创建一个新的Segue到Authenti cateViewController现场
  5. 手动从viewDidLoad中的UITabBarController的子类调用SEGUE

当我运行模式SEGUE不火的应用,显示了的UITabBarController的第一个视图控制器,我得到在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 

回答

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]; 
} 
+1

这样的工作,但我觉得这是一个相当不雅的解决方案。这是完成这个的唯一方法吗? “执行SegueWithIdentifer:”呼叫应该放在其他地方吗? – blundin 2013-03-25 15:42:31

+0

嘿@blundin请尝试设置afterDelay:0.0并检查。让我知道如果作品顺利 – 2013-03-25 15:44:53

+1

是的,这工作顺利。谢谢。 – blundin 2013-03-27 02:18:27

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 

这将触发Segue公司pushToMainView当你的消息[controller pushToMainView]但持有回来,直到视图被加载。

3

如何在viewDidAppear中简单调用您的代码?

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

不过,我还是不满意这些解决方案,因为原来的观点仍显示为几分之一秒。

有关如何显示新模式而不显示原始视图的任何想法?

相关问题