2014-01-05 30 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 ...;) –

相關問題