如何选择加载哪个Storyboard

时间:2014-04-20 13:56:00

标签: ios iphone storyboard

我需要根据运行该应用的iPhone型号选择要加载的MainViewController.h。使用下面的代码,我区分iPhone 4 / 4S和5 / 5S / 5C。由于自动布局,我需要创建两个mainViewController.h,然后根据手机的型号选择。我该怎么做呢?这是我的代码:

#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");

    }
}

所以请告诉我如何选择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];
}

1 个答案:

答案 0 :(得分:1)

要指定要加载的故事板,请使用AppDelegate application:didFinishLaunchingWithOptions:方法执行此操作。

使用其故事板名称加载与屏幕大小相对应的故事板,例如:

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