0

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.

Josh Liptzin
  • 746
  • 5
  • 12

1 Answers1

3

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

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    Also I don't think you should be including the suffix `.storyboard` in the string name of the storyboard. – matt Mar 17 '14 at 23:39
  • The extension might indeed be the problem. See [UIStoryboard documentation](https://developer.apple.com/library/ios/documentation/uikit/reference/UIStoryboard_Class/Reference/Reference.html#//apple_ref/occ/clm/UIStoryboard/storyboardWithName:bundle:) "Parameters *name*: The name of the storyboard resource file **without the filename extension**.". – Matthias Bauch Mar 18 '14 at 01:50
  • This answer is indeed helpful, thanks matt. As for the extension, I know about that issue, I just added that for the purpose of the question to make sure everyone understood that it was a storyboard file, but looks like I just added more confusion. – Josh Liptzin Mar 18 '14 at 02:59