1

I'm creating some code which I've sorted into a framework - purely because I want to reuse this code elsewhere (other osx apps - not iOS).

Inside my Resources folder I created a xib file. One level higher in my root I have the corresponding Controller file.

If my client app is using storyboards - how would I go about loading this view? Note I did manage to access properties on the mentioned controller so I did all the hook up etc as described in Apple docs and elsewhere.

Thanks in advance!

  • Just a note. I have a ViewController which was created by default in the host/client app. I've set the controller to inherit from the controller class defined in my framework (the one associated with the view I wish to load) –  Mar 06 '15 at 07:47

2 Answers2

2

You have to put the resources in a *.bundle, not a framework. Just create a new target that's a bundle.

Then load the nibs with:

NSString *resourceBundlePath = [[NSBundle mainBundle] pathForResource:@"myBundle" ofType:@"bundle"];
NSBundle *resourceBundle = [NSBundle bundleWithPath:resourceBundlePath];

// load View Controller from that
UIViewController *vc = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:resourceBundle];
michal.ciurus
  • 3,616
  • 17
  • 32
  • Thanks Michal. Yea I was pointed in this direction after someone, somewhere else mentioned 'dynamic libraries'. It sounds to me that bundles are the predecessor to frameworks (eventhough Apple have been using them for some time) -- however as you've mentioned and showed you will access them in the same way. Appreciate your help though :) –  Mar 09 '15 at 08:22
  • No problem. For future reference: you can't use dynamic libraries (*.dylib) in iOS because you will get rejected from the app store. http://stackoverflow.com/questions/26504617/will-appstore-reviewers-allow-us-to-use-dynamic-library-in-ios8 – michal.ciurus Mar 09 '15 at 08:25
  • let bundle = NSBundle(identifier:"com.moblejoseph.New") let controller = UIViewController(nibName:"MyViewController", bundle: bundle!) presentViewController(controller, animated: true, completion: nil) presentViewController(controller, animated: true, completion: nil) – Kiran P Nair Mar 15 '16 at 12:42
1

Solution: Here is a sample to load xib to create a view in the framework:

  1. Create a new bundle (eg: MyBundle.bundle) and put your all resources (such as: xib, images, audio files, etc) into the new bundle.
  2. Load xib:

    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
    NSString* path = [resourcePath 
    stringByAppendingPathComponent:@"MyBundle.bundle"];
    NSBundle *bundle = [NSBundle bundleWithPath:path];
    if(bundle)
    {
        self.view = [[bundle loadNibNamed:@"MyXibNameWithoutSuffix" owner:nil options:nil] lastObject];
    } else {
        self.view = [[[NSBundle mainBundle] 
    loadNibNamed:@"MyXibNameWithoutSuffix" owner:nil options:nil] lastObject];
    }
    
  3. Load image:

    UIImage* image = [UIImage imageNamed:@"MyBundle.bundle/MyImageName"];
    if(nil == image) {
       image = [UIImage imageNamed:@"MyImageName"];
    }
    
  4. If you're using cocopods, please make sure the s.resources has been set in .podspec file.

    s.resources = "MyBundle.bundle/**/*.{png,jpeg,jpg,storyboard,xib,xcassets,plist}"
    
xuzepei
  • 1,119
  • 11
  • 9