2014-01-05 34 views
1

我对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; 
} 

我在代码中的错误面对,如下所示: enter image description here

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

这里也是我的两个视图控制器的照片,我想他们是最初的enter image description here视图,该视图后,该应用程序已经加载一次:

如果提供任何帮助:

  • 第一视图控制器是一个UIViewController

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

    • 这就是所谓SWRevealViewController(在身份检查器中的自定义类被命名为“SWRevealViewController”)
    • 我已经实现SWRevealViewController.h和.m文件对于这个班级也是如此。

回答

2


可以使用restorationIdentifier,这是正确的故事板标识上面,它是一个UIViewController属性。

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

enter image description here

+0

@achievelimitless这两个答案对我的问题都是正确的。谢谢您的帮助! –

+0

@iDev只是一个问题。所以字符串@“<控制器ID>”,我把我的故事板ID或我的恢复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; 
    } 

让我知道如果你需要更多的帮助

+0

我不是小气,你的答案是完全可以接受的,以及,我只是感到你做了什么困惑与你的情况为你的if语句。如果我选择使用这个,这是否意味着我必须替换if语句的条件? –

+0

'code if([[NSUserDefaults standardUserDefaults] boolForKey:@“HasLaunchedOnce”])' –

+0

我试图给你一个通用应用程序的通用代码。我的if条件检查设备是iPad还是iPhone。你不需要在这里替换你的条件。对于iPhone IF条件执行,&为iPad ELSE之一。至少你可以投票的答案Yaar ...;) –

相关问题