为iphone 4和5创建单独的故事板

时间:2013-08-12 09:31:24

标签: iphone ios objective-c xcode storyboard

我正在尝试创建2个故事板,一个用于iPhone 4,一个用于iPhone 5.我希望在启动时检测用户正在使用哪个设备。我已经使用了以下代码并在我的app delegate.m中实现了它,但收到错误:

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;

    }

我可能需要导入一些东西来修复错误吗?

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

也许这可能只是一个错误,但你在didFinishApplicationLaunchingWithOptions中输入了一个动作空白?

您是否尝试将所有内容都放入didFinishApplicationLaunchingWithOptions中而不使用此操作无效?