2013-08-12 31 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

相關問題