2012-10-27 172 views
9

我使用下面的代碼在我AppDelegate.m檢測用戶使用的設備:加載不同的故事板開始

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) 
    { 
     CGSize result = [[UIScreen mainScreen] bounds].size; 
     if(result.height == 480) 
     { 
      NSLog(@"iPhone 3,5 Inch"); 

      [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil]; 



     } 
     if(result.height == 568) 
     { 
      NSLog(@"iPhone 4 Inch"); 
      [UIStoryboard storyboardWithName:@"iPhone5-storyboard" bundle:nil]; 
     } 
    } 

    return YES; 
} 

但是,當我構建應用程序的NSLog的是示,但沒有故事板快到了......

  • 在部署信息的主要情節提要字段爲空,這樣的代碼決定什麼加載...

可以幫助我嗎?

THX和來自德國的問候

Laurenz :)

+1

你需要不同的故事板,因爲你可以切換任何理由在故事板編輯器中3.5英寸和4英寸佈局之間? – jrturton

+1

3.5英寸和4英寸佈局之間的切換隻是一種模擬,您無法將這種方法設計成不同的接口! –

回答

15

這裏是一個辦法,我現在在等後發現:

if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) 
    { // The iOS device = iPhone or iPod Touch 


     CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size; 
     UIViewController *initialViewController = nil; 
     if (iOSDeviceScreenSize.height == 480) 
     { // iPhone 3GS, 4, and 4S and iPod Touch 3rd and 4th generation: 3.5 inch screen (diagonally measured) 

      // Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone35 
      UIStoryboard *iPhone35Storyboard = [UIStoryboard storyboardWithName:@"Storyboard_iPhone35" bundle:nil]; 

      // Instantiate the initial view controller object from the storyboard 
      initialViewController = [iPhone35Storyboard instantiateInitialViewController]; 
     } 

     if (iOSDeviceScreenSize.height == 568) 
     { // iPhone 5 and iPod Touch 5th generation: 4 inch screen (diagonally measured) 

      // Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone4 
      UIStoryboard *iPhone4Storyboard = [UIStoryboard storyboardWithName:@"Storyboard_iPhone4" bundle:nil]; 

      // Instantiate the initial view controller object from the storyboard 
      initialViewController = [iPhone4Storyboard instantiateInitialViewController]; 
     } 

     // Instantiate a UIWindow object and initialize it with the screen size of the iOS device 
     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

     // Set the initial view controller to be the root view controller of the window object 
     self.window.rootViewController = initialViewController; 

     // Set the window object to be the key window and show it 
     [self.window makeKeyAndVisible]; 

    } else if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) 

    { // The iOS device = iPad 

     UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController; 
     UINavigationController *navigationController = [splitViewController.viewControllers lastObject]; 
     splitViewController.delegate = (id)navigationController.topViewController; 

    } 
+0

但是在哪裏放這個代碼? –

+1

當然在AppDelegate.m ;-) –

+0

我想通了...這是如此明顯! hahahaha –