4

I created a bundle file and imported the bundle file into my project. There is a storyboard in this bundle file and I would like to create view from this storyboard. I have been trying to find out correct way to do this for 2 days, but got no luck. It always shows "'Could not find a storyboard named 'StoryboardA' in bundle NSBundle" error. I have a sample project here : https://github.com/tsunglintsai/Storyboard-In-Resource-Bundle.

Following is code I tried.

NSBundle *resourceBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"bundlefilename" ofType:@"bundle"]];
[resourceBundle load];
NSLog(@"resourceBundle:%@",resourceBundle);

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"StoryboardA" bundle:resourceBundle];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"myViewController"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];

-- I can't find answer from following similar question posts:

How to load storyboard file from custom bundle in IOS app?

Xcode Copy Bundle Resources can't find files

"Could not find a storyboard named 'MainStoryboard' in bundle NSBundle"

Community
  • 1
  • 1
Tsung Lin Tsai
  • 661
  • 1
  • 10
  • 22

2 Answers2

7

You need to create a proper bundle, not just rename a directory with a .bundle extension.

Your resources such as the storyboard need to be in the Contents/Resources/ subdirectory, and you need an Info.plist in the Contents/ directory as well.

You can generate a basic template by creating a new target and picking Bundle from OS X > Framework & Library. Then archive it and look in the archive.

There's probably a more streamlined way of doing it with target dependencies, but it wasn't obvious to me how to make Xcode build the bundle with a normal build command, it only seems to work with archiving.

For more information, see Loadable Bundles in Apple's Bundle Programming Guide.

Jim
  • 72,985
  • 14
  • 101
  • 108
  • Hi Jim, thank you very much for the answer. It works after I follow your suggestion. I have updated git hub project so if anyone encounters the same issue can have a place to reference to. – Tsung Lin Tsai Nov 18 '13 at 22:17
-1

If you use loadAndReturnError: instead of just load, you get this error message:

Error Domain=NSCocoaErrorDomain Code=4 "The bundle “bundlefilename.bundle” couldn’t be loaded because its executable couldn’t be located." UserInfo=0xb8c8720 {NSLocalizedRecoverySuggestion=Try reinstalling the bundle., NSLocalizedFailureReason=The bundle’s executable couldn’t be located., NSLocalizedDescription=The bundle “bundlefilename.bundle” couldn’t be loaded because its executable couldn’t be located., NSBundlePath=/Users/user1/Library/Application Support/iPhone Simulator/7.0.3/Applications/2952FC15-F092-4F04-9BD3-902A923BC692/StoryboardInBundle.app/bundlefilename.bundle}

So it appears that there's something wrong with your bundle (although I don't have any experience with loading bundles).

If your object is to use the storyboard and image contained in that bundle, you can open the bundle in the finder, click on "show package contents", and drag those two files into your project. Then you don't need to load the bundle, and it can be removed from the project.

rdelmar
  • 103,982
  • 12
  • 207
  • 218