Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - loading xib from bundle in Documents directory

Tags:

ios

bundle

I've been working on getting "custom skinnable" interfaces working for my iOS app by bundling up xibs and downloading bundles, and loading the xibs in those bundles.

I have followed the very useful instructions here to get it almost working.

loading NSBundle files on iOS

I have the bundle downloading, and unzipping and can see the xib file. I have not used any of the TestViewController code in the example, but am instead doing this when creating the view controller using the just downloaded xib:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"gg.bundle"];
    NSBundle *bundle = [NSBundle bundleWithPath:filePath];

    if ( !bundle )
    {
        NSLog(@"Error getting bundle");
    }

    MyViewController *vc = [MyViewController new];
    [vc initWithNibName:@"CustomDownloadedXib" bundle:bundle];

When I go to push this view controller onto my stack, I get this error:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (not yet loaded)' with name 'CustomDownloadedXib''

The issue seems to be that my bundle is "not yet loaded." I tried forcing my bundle to load by calling:

[bundle load]

but that doesn't help.

Is my approach here incorrect? This seems like the intuitive way to do it.

Thanks in advance!

like image 902
Mark Avatar asked Jun 24 '11 04:06

Mark


People also ask

What is XIB file in iOS?

From the point of view of the UI designer, XIB files contain the views or windows that you design in Interface Builder – the controls, their layout and properties, and their connections to your code. It is important to understand that on a technical level, XIB files are stored object graphs.

How do I copy an XIB file?

yes , you can copy the the contents of the xib file by just (command+c) and paste it into another xib file by using (command +v) but by simply cop and pasting you won't get the references you need to go the properties in xib file there you need to give the class name for which you have created a new xib file and then ...

Which data format do XIB files use?

XIB files are in XML format to make them easier to use with version control systems and text-based tools. Let's go ahead and start using IB. To do this, first create an iOS App using the Single View Application iOS Project template in Xcode.


2 Answers

First of all if a bundle is not created properly it will not get loaded. So in-order to create a proper bundle below are the steps for creating a bundle:

  1. Add a new target by choosing a template named bundle under OS X -> Framework & Libraries.

  2. Select newly created target and change BaseSDK from OSX to Latest iOS.

  3. Add .xibs, images or other resources which you want to use it from bundle in Build Phrases -> Copy Bundle Resources.

  4. Add CoreFoundation framework in Build Phrases -> Link binary with Libraries.

  5. Compile the target choosing iOS Device.

  6. Save the newly created bundle from Products directory to some place.

Now copy that bundle into your main project. Load a bundle using below code:

NSString *path = [[NSBundle mainBundle] pathForResource:@"BundleName" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:path];"

You are now set with the new bundle.

like image 191
Dharmesh Siddhpura Avatar answered Nov 16 '22 17:11

Dharmesh Siddhpura


Following the instructions here works:

loading NSBundle files on iOS

I was just creating the bundle incorrectly!

You need to create the bundle in Xcode though because if you don't, it won't be loaded. To create, Add a New Target to your project, choose Framework & Library -> Bundle, and link to Core Foundation. Then add the xibs you want to upload to the web to the target. Build target, reveal it in Finder, compress, and upload!

like image 22
Mark Avatar answered Nov 16 '22 18:11

Mark