2013-08-12 32 views
1

我正在尝试创建2个故事板,一个用于iPhone 4,另一个用于iPhone 5.我希望在启动用户正在使用的设备时检测它。我用下面的代码,并在我的应用程序delegate.m实现,但收到错误消息:为iphone 4和5创建单独的故事板

Use of undeclared identifier "initializeStoryBoardBasedOnScreenSize" 

下面是我使用的代码:

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


-(void)initializeStoryBoardBasedOnScreenSize { 

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


     CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size; 

     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 
      UIViewController *initialViewController = [iPhone35Storyboard 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]; 
     } 

     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 
      UIViewController *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

您可以检查http://stackoverflow.com/questions/12696242/how-to-switch-to-different-storyboard-for-iphone-5并在**应用程序didFinishLaunchingWithOptions **中调用您的方法。 – Manthan

+0

这就是我上面所做的,对吧? – user2667306

+0

我认为你需要在**应用程序didFinishLaunchingWithOptions **中调用方法,如Martin给出的答案。 – Manthan

回答

1

您试图定义的方法中application:didFinishLaunchingWithOptions:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    -(void)initializeStoryBoardBasedOnScreenSize { 
     // ... your code ... 
    } 
    return YES; 
} 

这不是你想要的东西,顺便说一句。 Objective-C中不支持嵌套函数(或方法) 。

你大概的意思是定义一个方法和调用它 内application:didFinishLaunchingWithOptions:

-(void)initializeStoryBoardBasedOnScreenSize { 
    // ... your code ... 
} 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    [self initializeStoryBoardBasedOnScreenSize]; 
    return YES; 
} 
+0

当我这样做并启动应用程序时,它不会加载支持iPhone 5的故事板。它正在加载我的主要故事板。 – user2667306

+0

@ user2667306:我会建议使用调试器。在'initializeStoryBoardBasedOnScreenSize'中设置断点,并单步执行代码。 iOSDeviceScreenSize有什么价值?哪个if块被执行?等等... –

+0

它的工作原理!但我添加了一个新的故事板,并且我无法将任何操作添加到新的故事板,为什么? – user2667306

0

也许它可能只是在这里一个错误,但你进入didFinishApplicationLaunchingWithOptions动作无效?

您是否尝试过将didFinishApplicationLaunchingWithOptions中的所有内容都包含进来,而无需使用此操作void?

+0

我试过了,但它给了我错误 – user2667306

相关问题