在应用程序启动时从故事板中选择备用第一视图控制器 [英] selecting alternative first view controller from story board at application startup

查看:100
本文介绍了在应用程序启动时从故事板中选择备用第一视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用iOS编程,到目前为止,我在这里找到的教程和答案对于前进起了很大的帮助。然而,这个特殊问题一直困扰着我,我无法找到感觉正确的答案。

I've just started on iOS programming and so far the tutorials and answers I found here have been a great help to move forward. However, this particular problem has been bumming me all night and I can't find an answer that "feels right".

我正在编写一个连接到a的应用程序远程服务和用户需要先登录才能使用它。当他们开始使用应用程序时,他们的第一个视图应该是登录对话框;当他们之前通过身份验证时,他们会立即看到概述页面。

I'm writing an application that connects to a remote service and the users need to sign in before they can use it. When they start using the application, their first view should be the sign in dialog; when they've authenticated before, they immediately see the overview page.

该项目使用故事板 - 我认为这是一个很棒的功能 - 因此大多数代码选择和加载根视图控制器已经处理好了。我认为添加逻辑的最佳位置是应用程序:didFinishLaunchingWithOptions: AppDelegate的方法

The project uses story boards - which I think is a great feature - so most of the code that selects and loads the root view controller is already taken care of. I thought the best place to add my logic is the application:didFinishLaunchingWithOptions: method of the AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
      (NSDictionary *)launchOptions
{
    // select my root view controller here based on credentials present or not
    return YES;
}

但这提出了两个问题:


  1. 在这个特定的委托方法中,根据故事板已经选择(并加载?)根视图控制器。我可以移动到加载过程中的早期位置来覆盖第一个视图控制器选择,还是会使问题变得不必要?

  1. Inside this particular delegate method, the root view controller has already been selected (and loaded?) based on the story board. Could I move to an earlier spot in the loading process to override the first view controller selection or would that needlessly complicate matters?

要覆盖第一个视图控制器I需要一个对故事板的引用,但我找不到比使用 storyboardWithName:bundle: UIStoryboard 。这感觉不对,应用程序应该已经有了故事板的参考,但是如何访问呢?

To override the first view controller I need a reference to the story board, but I couldn't find a better way than to use the storyboardWithName:bundle: constructor of UIStoryboard. That feels wrong, the application should already have a reference to the story board, but how can I access it?

更新

我找到了第二个问题,因为我在这里找到了答案:

I worked out the second issue I was having, as I found my answer here:

UIStoryboard:什么是正确的方法获取活动故事板?

NSBundle *bundle = [NSBundle mainBundle];
NSString *sbFile = [bundle objectForInfoDictionaryKey:@"UIMainStoryboardFile"];
UIStoryboard *sb = [UIStoryboard storyboardWithName:sbFile bundle:bundle];

以上将创建一个新的故事板实例;为了获得活动实例,它更加简单:

The above will create a new story board instance; to get the active instance, it's a whole lot simpler:

UIStoryboard *sb = [[self.window rootViewController] storyboard];

在故事板文件本身中,您必须为要加载的视图设置标识符,例如的LoginDialog 。然后你实例化这样的视图:

In the story board file itself you have to set an identifier for the view you wish to load, e.g. LoginDialog. Afterwards you instantiate the view like this:

LoginViewController *login = [sb instantiateViewControllerWithIdentifier:@"LoginDialog"];
[self.window setRootViewController:login];

在另一个视图控制器中,以下内容足够:

Within another view controller, the following suffices:

UIStoryboard *sb = self.storyboard;
LoginViewController *login = [sb instantiateViewControllerWithIdentifier:@"LoginDialog"];
[self presentViewController:login animated:NO completion:nil];


推荐答案

您可以重置窗口的根视图控制器

You can just reset the root view controller of the window

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
      (NSDictionary *)launchOptions
{
   if(your_condition) {
       UIViewController *newRoot = [your implementation];
       self.window.rootViewController = newRoot;
   }
   return YES;
}

这对我有用,Xcode5.0.1

This is worked for me, Xcode5.0.1

这篇关于在应用程序启动时从故事板中选择备用第一视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆