Question

I need to choose which MainViewController.h to load depending on the iPhone model that the app is running on. Using the code below, I distinguish between iPhone 4/4S and 5/5S/5C. Because of the auto layout, I will need to create two mainViewController.hs and then choose depending on the model of the phone. How do I do this? This is the code I have:

#define IS_IPHONE_5         ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (IS_IPHONE_5) {

        NSLog(@"Iphone 5");


    } else {

        NSLog(@"Iphone4");

    }
}

So please tell me how to chose MainViewController.h.

AppDelegate.m:

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

    [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|
     UIRemoteNotificationTypeAlert|
     UIRemoteNotificationTypeSound];
    // Override point for customization after application launch.
    return YES;

    UIStoryboard *storyboard;

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

    if (screenSize.height == 568) {
        // iPhone 5
        storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone5" bundle:nil];
    } else {
        // iPhone 4
        storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone4" bundle:nil];
    }

    UIViewController *initialViewController = [storyboard instantiateInitialViewController];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController  = initialViewController;
    [self.window makeKeyAndVisible];
}
Was it helpful?

Solution

To specify which storyboard to load, do so in your AppDelegate's application:didFinishLaunchingWithOptions: method.

Load the storyboard that corresponds to the screen size using its storyboard name, ex:

UIStoryboard *storyboard;

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

if (screenSize.height == 568) {
    // iPhone 5, i.e. 568 pixels high
    storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone5" bundle:nil];  
} else {
    // earlier model iPhones, i.e. 480 pixels high
    storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone4" bundle:nil];      
}

UIViewController *initialViewController = [storyboard instantiateInitialViewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController  = initialViewController;
[self.window makeKeyAndVisible];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top