【问题标题】:Create separate storyboards for iphone 4 and 5为 iphone 4 和 5 创建单独的故事板
【发布时间】:2013-08-12 09:31:24
【问题描述】:

我正在尝试创建 2 个故事板,一个用于 iPhone 4,一个用于 iPhone 5。我希望在启动时检测到用户正在使用哪个设备。我已使用以下代码并在我的应用程序 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;

    }

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

【问题讨论】:

  • 你可以检查stackoverflow.com/questions/12696242/…并在application didFinishLaunchingWithOptions中调用你的方法。
  • 这就是我上面所做的,对吧?
  • 我认为您需要在 application didFinishLaunchingWithOptions 中调用方法,正如 Martin 下面给出的答案。
  • 当我在 iphone 5 设备上启动应用程序时,应用程序只显示我的第一个故事板,而不是在您使用 iPhone 5 时应该启动的故事板
  • 放置断点并检查它在哪个条件下运行。您将知道是哪一行导致了问题,并确保您输入了 iPhone4 和 5 的正确名称。

标签: iphone ios objective-c xcode storyboard


【解决方案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;
}

【讨论】:

  • 当我这样做并启动应用程序时,它没有加载支持 iPhone 5 的情节提要。它正在加载我的主故事板。
  • @user2667306:我建议使用调试器。在initializeStoryBoardBasedOnScreenSize 中设置断点并单步执行您的代码。 iOSDeviceScreenSize 有什么价值?执行哪个 if 块?等等……
  • 有效!但是我添加了一个新的故事板,但我无法向新的故事板添加任何动作,为什么?
  • @user2667306:如果出现您自己无法解决的新问题,我建议您发布一个新问题。
【解决方案2】:

也许这只是一个错误,但您在 didFinishApplicationLaunchingWithOptions 中输入了一个操作无效?

您是否尝试将所有内容都放入 didFinishApplicationLaunchingWithOptions 而不简单地使用此操作 void?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-08
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多