【问题标题】:display a modal view on ipad application startup在 ipad 应用程序启动时显示模式视图
【发布时间】:2012-03-26 04:24:39
【问题描述】:

我想在启动 ipad 应用程序时有条件地显示登录屏幕。我不想让它成为默认 segue 的一部分,因为他们只需要定期登录,而不是每次登录。

我的问题有numerousexamples,但它们似乎都早于ios5。然而,当我使用故事板时,似乎没有任何效果。

为了简化它的本质, * 创建一个新的单视图应用程序,使用情节提要 * 向情节提要添加一个新的视图控制器,给它一个标识符“loginScreen” * 在每个视图上放置一个文本标签,以便在视觉上区分它们。 * 在 appDelegate 中:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIStoryboard *storyboard = [self.window.rootViewController storyboard];
    UIViewController *loginController = [storyboard instantiateViewControllerWithIdentifier:@"loginScreen"];
    [self.window.rootViewController presentModalViewController:loginController animated:TRUE];

    return YES;
}

从我所看到的示例来看,这应该可行。但它仍然始终显示原始 rootViewController 的视图。不过没有错误。

谁能指出我遗漏的(可能很小的)东西?

【问题讨论】:

    标签: ios5 uiapplicationdelegate presentmodalviewcontroller uistoryboard


    【解决方案1】:

    didFinishLaunching 方法中,应用程序未处于活动状态。

    放置这个的正确位置是

    - (void)applicationDidBecomeActive:(UIApplication *)application
    {
        UIStoryboard *storyboard = self.window.rootViewController.storyboard;
        UIViewController *loginController = [storyboard instantiateViewControllerWithIdentifier:@"loginScreen"];
        [self.window.rootViewController presentModalViewController:loginController animated:NO];
    }
    

    【讨论】:

      【解决方案2】:

      @deafgreatdane:您的解决方案将在每次应用程序从后台状态变为活动状态时以模态方式呈现视图控制器(这可能是可取的)。

      在我的情况下(使用它来显示一次性启动屏幕)我会在该解决方案中添加一个dispatch_once 以确保模式启动屏幕只会显示一次:

      - (void)applicationDidBecomeActive:(UIApplication*)application
      {
         static dispatch_once_t onceToken;
      
         dispatch_once( &onceToken, ^
                       {
                          SomeLaunchViewController* launchViewController = [[SomeLaunchViewController alloc] init];
                          [self.window.rootViewController presentViewController:launchViewController animated:NO completion:NULL];
                       } );
      }
      

      此代码 sn-p 使用 ARC。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-06
        • 2021-12-11
        • 2017-01-27
        • 2011-08-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多