在 Xcode 中使用多个 Storyboard [英] Using multiple Storyboards in Xcode

查看:31
本文介绍了在 Xcode 中使用多个 Storyboard的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了 5 个额外的故事板,并删除了主故事板.这是因为我希望 iPhone 和 iPad 有 5 个单独的故事板来匹配 5 个不同的屏幕尺寸.到目前为止,我有一款适用于 iPhone 3.5 英寸,一款适用于 iPhone 4 英寸,一款适用于 iPhone 4.7 英寸,一款适用于 iPhone 5.5 英寸,一款适用于 iPad.我已经将代码放在下面以将其全部链接起来并使其工作.但是,当您尝试构建项目时它不起作用.没有错误,但假设我进入 iPhone 3.5 英寸故事板并添加一个 UIViewController 和一个按钮或标签,然后当您构建项目时,它会转到您的启动屏幕,然后它不会从那里做任何事情.我已将起始箭头放在 UIViewController 中,但除了启动屏幕之外,我无法显示任何内容.我已经在所有的故事板和他们的模拟器上尝试过这个.我想知道我是否遗漏了代码中的某些内容,但我不确定.

I have created 5 extra Storyboards and I have deleted the main storyboard. This is because I would like to have 5 separate storyboards for the iPhone and iPad to match 5 different screen sizes. So far I have one for the iPhone 3.5 inch, one for the iPhone 4 inch, one for the iPhone 4.7 inch, one for the iPhone 5.5 inch and one for the iPad. I have put the code bellow in to link it all up and make it work. However, it doesn't work when you try and build the project. There is no errors but lets say I go into the iPhone 3.5 inch storyboard and I add a UIViewController and a Button or label, then when you build the project it goes to your launch screen and then it doesn't do anything from there. I have put the starting arrow in at the UIViewController but I cannot get anything to come up apart from the launch screen. I have tried this on all the storyboards and their simulators. I am wondering if I have missed something out in the code but I am not sure.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIStoryboard *storyboard = nil;
if ([[UIDevice currentDevice] userInterfaceIdiom] ==UIUserInterfaceIdiomPad) {
    storyboard = [UIStoryboard storyboardWithName:@"Storyboard_iPad" bundle:nil];//iPad
} else {
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    if (screenSize.height == 480){
        storyboard = [UIStoryboard storyboardWithName:@"Storyboard_iPhone4S" bundle:nil];//iPhone 3.5inch
    } else
        if (screenSize.height == 568){
            storyboard = [UIStoryboard storyboardWithName:@"Storyboard_iPhone5/5C/5S" bundle:nil];//iPhone 4inch
        }
        else
        { if (screenSize.height == 667){
            storyboard = [UIStoryboard storyboardWithName:@"Storyboard_iPhone6" bundle:nil];//iPhone 4.7inch
            } else
                if (screenSize.height == 736){
                    storyboard = [UIStoryboard storyboardWithName:@"Storyboard_iPhone6Plus" bundle:nil];//iPhone 5.5inch
                } else
                    //default storyboard
                storyboard = [UIStoryboard storyboardWithName:@"Main.Storyboard" bundle:nil];
            }
}
    self.window.rootViewController = [storyboard instantiateInitialViewController];
    [self.window makeKeyAndVisible];

return YES;
}

推荐答案

在您的项目中找到您的 info.plist 文件并删除名为Main storyboard file base name"的行.

Find your info.plist file in your project and remove the line called "Main storyboard file base name".

此外,您忘记创建窗口(归功于 rdelmar).

Also, you forgot to create the window (credits to rdelmar).

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

我强烈建议您查看自动布局尺寸类.使用这两个,您可以在一个 Storyboard 中支持所有屏幕尺寸.一开始有点难,但从长远来看绝对值得.

I strongly advise you to look into Auto Layout and Size Classes. Using these two, you can support all screen sizes within one single Storyboard. It's a bit hard to get in the beginning, but it'll definitely be worth it in the long run.

最近的 WWDC 上有一个关于此的视频,名为 Building带有 UIKit 的自适应应用.

There's a video about this from the most recent WWDC called Building Adaptive Apps with UIKit.

这篇关于在 Xcode 中使用多个 Storyboard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆