在应用程序启动时从情节提要中选择替代的第一

selecting alternative first view controller from story board at application startup(在应用程序启动时从情节提要中选择替代的第一个视图控制器)

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

问题描述

我刚刚开始使用 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".

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

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.

该项目使用故事板——我认为这是一个很棒的特性——所以选择和加载根视图控制器的大部分代码都已经处理好了.我认为添加逻辑的最佳位置是 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?

要覆盖第一个视图控制器,我需要引用故事板,但我找不到比使用 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];

在故事板文件本身中,您必须为要加载的视图设置标识符,例如登录对话框.然后你像这样实例化视图:

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

这篇关于在应用程序启动时从情节提要中选择替代的第一个视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在应用程序启动时从情节提要中选择替代的第一

基础教程推荐