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")

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

9 Comments

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
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.
But there is a catch here ankit, all my nib files are in a framework. How can i refer them from a bundle perspective?
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.. stackoverflow.com/questions/707429/… at the second answer there by justicle.
+1 for a good link.... i did exactly as specified in that link.. but, still no luck.. The crash still persists... :(
|
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.