【问题标题】:Loading a different view controller after app has already launched应用程序启动后加载不同的视图控制器
【发布时间】:2014-01-05 03:38:11
【问题描述】:

我对 Xcode 有点陌生。

我有一个关于在应用启动后加载不同的问题。我已经查看了过去的问题,例如我的问题,并在 appdelegate.m 文件中看到了这个问题的答案,特别是 didFinishWithLaunchingOptions 方法。

但是,答案提供的代码对我没有任何补救措施,因为我使用的是storyboard,并且从 Xcode 5 开始,initwithnib 方法不能再使用。

如果我的问题对你来说不是很清楚,我在appdelegate.m 中的代码如下所示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
 {
  // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
    {
        NSLog(@"not first launch");
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController"  bundle:nil];
        self.window.rootViewController = self.viewController;
    }
    else
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
        [[NSUserDefaults standardUserDefaults] synchronize];

        self.initialViewController = [[InitialViewController alloc]  initWithNibName:@"InitialViewController" bundle:nil];
        self.window.rootViewController = self.InitialViewController;
        NSLog(@"first launch");
    }
    [self.window makeKeyAndVisible];



UIImage *navBackgroundImage = [UIImage imageNamed:@"nav_bg"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];

[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
                                                       [UIColor colorWithRed:10.0/255.0 green:10.0/255.0 blue:10.0/255.0 alpha:1.0], UITextAttributeTextColor,
                                                       [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],UITextAttributeTextShadowColor,
                                                       [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                       UITextAttributeTextShadowOffset,
                                                       [UIFont fontWithName:@"Avenir Next" size:20.0], UITextAttributeFont, nil]];

UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
pageControl.backgroundColor = [UIColor whiteColor];

return YES;
}

我在代码中遇到错误,如下所示:

所以,我的问题几乎是我试图在我的 appdelegate.m 的方法中实现这个代码块,但我遇到了一些错误,我不知道它们为什么会发生。

这也是我的两个视图控制器的图片,我希望它们成为初始 视图和应用程序加载一次后的视图:

如果它提供任何帮助:

  • 第一个视图控制器是UIViewController

    • 它被称为ViewController(在身份检查器中,自定义类被命名为“Viewcontroller”)
    • 我已经为这个类实现了ViewController.h 和.m 文件。
  • 第二个视图控制器也是一个 UIViewController

    • 它被称为SWRevealViewController(在身份检查器中,自定义类被命名为“SWRevealViewController”)
    • 我也为这个类实现了SWRevealViewController.h 和.m 文件。

【问题讨论】:

    标签: ios iphone objective-c xcode uiviewcontroller


    【解决方案1】:


    您可以使用 restoreIdentifier,它位于 Storyboard 标识符的正上方,是一个 UIViewController 属性。

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                             bundle: nil];
    MyViewController *controller = (MyViewController*)[mainStoryboard 
                    instantiateViewControllerWithIdentifier: @"<Controller ID>"];
     self.window.rootViewController = controller;
    

    【讨论】:

    • @achievelimitless 两个答案对我的问题都是正确的。感谢您的帮助!
    • @iDev 再问一个问题。那么字符串@"",我是放我的storyboard id还是我的restore id?
    【解决方案2】:

    这个怎么样?

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
    
        CustomVC *rootController = [storyboard instantiateViewControllerWithIdentifier:@"MyVC"];
        self.window.rootViewController = rootController;
        }
    else{    
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil];
        CustomVC *rootController = [storyboard instantiateViewControllerWithIdentifier:@"MyVC"];
        self.window.rootViewController = rootController;
        }
    

    如果您需要更多帮助,请告诉我

    【讨论】:

    • 我并不是要刻薄,您的回答也完全可以接受,我只是对您对 if 语句的条件所做的事情感到困惑。如果我选择使用它,这是否意味着我必须用条件替换我的 if 语句?
    • 'code if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])'
    • 我试图为您提供通用应用程序的通用代码。我的 if 条件检查设备是 iPad 还是 iPhone。你不需要在这里更换你的条件。对于 iPhone IF 条件执行,& 对于 iPad ELSE 之一。至少你可以投票给答案 Yaar ... ;)
    • 感谢您的回答,我将尝试实现您的代码,看看它是否有效。我不能投票的唯一原因是因为我没有 15 名声望......但是现在有了它,我可以投票了 ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多