1

I'm trying to create 2 storyboards, one for iPhone 4 and one for iPhone 5. I want it to be detected at launch which device the user is using. I've used the following code and implemented it in my app delegate.m, but receive the error:

Use of undeclared identifier "initializeStoryBoardBasedOnScreenSize"

Here's the code I've used:

- (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;

    }

Is there maybe something I need to import to fix the error?

user2667306
  • 45
  • 1
  • 6
  • you can check http://stackoverflow.com/questions/12696242/how-to-switch-to-different-storyboard-for-iphone-5 and call your method inside **application didFinishLaunchingWithOptions**. – Manthan Aug 12 '13 at 09:44
  • That's what I've done above, right? – user2667306 Aug 12 '13 at 09:49
  • I think you need to call method in **application didFinishLaunchingWithOptions** as given the answer below by Martin. – Manthan Aug 12 '13 at 09:52
  • When I do and I launch the app on the iphone 5 device the app is only showing my first storyboard not the one that is supposed to launch when you use iPhone 5 – user2667306 Aug 12 '13 at 10:01
  • Put Breakpoint and check in which condition its going. You will know which line causes the problem and make sure you put the correct name from iPhone4 & 5. – Manthan Aug 12 '13 at 10:07

2 Answers2

1

You have tried to define a method inside application:didFinishLaunchingWithOptions::

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

This is not what you want and btw. nested functions (or methods) are not supported in Objective-C.

What you probably meant is to define a method and call it inside application:didFinishLaunchingWithOptions::

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

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [self initializeStoryBoardBasedOnScreenSize];
    return YES;
}
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • When I do this and launch the app, it's not loading the iPhone 5 supported storyboard. It's loading my main storyboard instead. – user2667306 Aug 12 '13 at 10:03
  • @user2667306: I would suggest to use the debugger. Set a breakpoint in `initializeStoryBoardBasedOnScreenSize` and single-step through your code. What value has iOSDeviceScreenSize? Which if-block is executed? And so on ... – Martin R Aug 12 '13 at 10:06
  • It works! but I've added a new storyboard and I can't add any actions to the new storyboard, why? – user2667306 Aug 12 '13 at 10:25
  • @user2667306: I would suggest that you post a new question if new problems arise that you cannot solve yourself. – Martin R Aug 12 '13 at 11:07
0

maybe it could just be an error here, but you're entering an action void in didFinishApplicationLaunchingWithOptions?

did you try to fit everything in didFinishApplicationLaunchingWithOptions without simply use this action void?

Ilario
  • 5,979
  • 2
  • 32
  • 46