从其他Xcode项目加载情节提要

问题描述:

我有两个不同但相关的项目.父项目A有一个情节提要.子项目B(在理论上扩展了A的功能)需要在其AppDelegate中实例化A的主故事板.在我的xcode工作区中,我将父项A包含在子项目B中,作为一个链接项目,我可以看到所有文件.我在application:didFinishLaunchingWithOptions中使用以下代码:

I have two different but related projects. Parent project A has a storyboard. Child project B, which (in theory) extends the functionality of A, needs to instantiate A's main storyboard in its AppDelegate. In my xcode workspace, I've included parent A within child project B, as a linked project and I can see all the files. I am using the following code in application:didFinishLaunchingWithOptions:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"iPhone.storyboard" bundle:[NSBundle mainBundle]];
MainViewController *vc = (MainViewController *)[storyboard instantiateInitialViewController];
_window.rootViewController = vc;
[self.window makeKeyAndVisible];

代码在运行时在storyboardWithName行中失败,我认为是因为iPhone.storyboard在B中不立即可用,并且也不知道在A中查找它.实际的Storyboard文件位于外部的其他文件夹中子项目B的项目文件夹在磁盘上.

The code fails at runtime at the storyboardWithName line, I assume because iPhone.storyboard is not available immediately within B, and it doesn't know to look for it within A. The actual storyboard file is located in a different folder outside child project B's project folder on disk.

与B和A没有关系.项目中的文件可以位于任何地方.您的错误是在运行时发生的.它与您正在构建和运行的 app 有关.想想这行说的是什么:

It has nothing to do with B and A. Files in the project can be located anywhere. Your error is at runtime. It has to do with the app you are building and running. Think about what this line says:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"iPhone.storyboard" bundle:[NSBundle mainBundle]];

因此,您在这里声称该应用程序的主捆绑包中有一个名为 iPhone.storyboard 的情节提要.但是没有.该应用程序正在构建中,您没有做任何事情来导致情节提要板作为该过程的一部分复制到该应用程序的捆绑包中.那就是你需要做的.

So you are claiming here that there is a storyboard called iPhone.storyboard inside this app's main bundle. But there isn't. The app is building and you aren't doing anything to cause the storyboard to be copied into the app's bundle as part of that process. That's what you need to do.

要将故事板放置在主捆绑包中,请将其添加到此应用程序的复制捆绑包资源"构建阶段.

To get the storyboard to be in the main bundle, add it to this app's Copy Bundle Resources build phase.

(当然,现在可能还有其他问题,例如,如果此情节提要中引用的类也不属于此应用程序.但这不是您的问题.)

(Now, of course, there may be other problems, e.g. if classes referred to in this storyboard are not also part of this app. But that's not what your question was.)