4

i have created a framework out of my existing code base and tried using it in a new code base. This worked fine. But my application is getting crashed when i try to access the nib files which is a part of my framework bundle. This is the code i am using to access the view controllers XIB file.

testViewController *controller = [[testViewController alloc] initWithNibName:@"testViewController" bundle:nil]; [self.view addSubview:controller.view];

The app is getting crashed without generating any error or crash report. Here aremy questions

1 - Can we add xib files in our framework

2 - If, yes, what is the correct way to add and access the nib(xib)files from a framawork(currently i have added them in the "compile sources" section of my "build phases" tab in "build settings")

A for Alpha
  • 2,904
  • 8
  • 42
  • 76

2 Answers2

4

You need to write the name of the bundle in which you nib. files are store so change your above code to ...

testViewController *controller = [[testViewController alloc] initWithNibName:@"testViewController" bundle:yourBundle]; [self.view addSubview:controller.view];

here your bundle is an object of type NSBundle. refer the NSBundle class for more info

Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115
  • testViewController *controller = [[testViewController alloc] initWithNibName:@"testViewController" bundle:@"FI"]; [self.view addSubview:controller.view]; I modified the code like this ankit. Here FI is the name of my newly created bundle/framework. But even now my app crashes with the the following error. "[__NSCFConstantString pathForResource:ofType:]: unrecognized selector sent to instance 0x6185c"... Any ideas – A for Alpha Dec 12 '11 at 09:43
  • you dont need to give a string here.. see what apple has to say.. "- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle" it needs to be an object of type NSBundle. – Ankit Srivastava Dec 12 '11 at 09:50
  • But there is a catch here ankit, all my nib files are in a framework. How can i refer them from a bundle perspective? – A for Alpha Dec 12 '11 at 10:10
  • sorry I misunderstood your question earlier.As far as I know you can't add .xib to static libraries, and personal frameworks are not supported by ios , you can get more info here.. http://stackoverflow.com/questions/707429/can-you-reference-xib-files-from-static-libraries-on-the-iphonelook at the second answer there by justicle. – Ankit Srivastava Dec 12 '11 at 10:11
  • +1 for a good link.... i did exactly as specified in that link.. but, still no luck.. The crash still persists... :( – A for Alpha Dec 12 '11 at 10:13
  • do you wish to ship it to a client or for personal use.? In case personal then just put the .xib in your resources folder manually. Even I failed at this , when I tried it earlier around 6 months ago. – Ankit Srivastava Dec 12 '11 at 10:16
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5768/discussion-between-a-for-alpha-and-ankit-srivastava) – A for Alpha Dec 12 '11 at 10:33
  • @i_raqz there is no work around for this. You will have to ship your bundle and static library separately. so the user will have to include both in his/her project – Ankit Srivastava May 22 '13 at 15:12
  • @AnkitSrivastava... thank you for answering..even i am trying the same approach..but then running into issues of loading images...can you please help...http://stackoverflow.com/questions/16695506/accessing-images-from-nsbundle – i_raqz May 22 '13 at 15:15
1

In swift 2 - For storyboard :

let bundle = NSBundle(identifier:"com.bundileName.Name")
    let storyboard = UIStoryboard(name:"Storyboard", bundle:bundle!)
    let controller = storyboard.instantiateViewControllerWithIdentifier("ViewControllerId") as UIViewController
    presentViewController(controller, animated: true, completion: nil)

For XIB:

let bundle = NSBundle(identifier:"com.bundileName.Name")
    if !(bundle == nil){
        let objtestViewController = testViewController(nibName: "testViewController", bundle: bundle)
        presentViewController(objtestViewController, animated: true, completion: nil)
    }

Swift 3 Storyboard:

 let bundle = Bundle(identifier:"com.bundileName.Name")
    let storyboard = UIStoryboard(name:"Storyboard", bundle:bundle!)
    let controller = storyboard.instantiateViewController(withIdentifier: "ViewControllerId") as UIViewController
    present(controller, animated: true, completion: nil)

Xib

let bundle = NSBundle(identifier:"com.bundileName.Name")
if !(bundle == nil){
    let objtestViewController = testViewController(nibName: "testViewController", bundle: bundle)
    present(objtestViewController, animated: true, completion: nil)
}

Here bundle name is the bundle id of framework.

Kiran P Nair
  • 2,041
  • 22
  • 31