简体   繁体   中英

Loading a storyboard from a different Xcode project

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

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.

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

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, eg if classes referred to in this storyboard are not also part of this app. But that's not what your question was.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM