Failed to access .nib files (XIB) from iOS framework

I created a structure from my existing code base and tried to use it in a new code base. This works great. But my application crashes when I try to access nib files that are part of my frame package. This is the code that I use to access the view manager XIB file.

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

The application crashes without generating an error or crash report. Here are some questions.

1 - Can we add xib files to our infrastructure

2 - If so, what is the correct way to add and access nib (xib) files from the framework (currently I have added them to the "Compiling Sources" section of my "build phase" in the "build settings")

+4
source share
2 answers

You need to write the name of the package in which you are using nib. files are stored, so change your above code to ...

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

here your package is an object of type NSBundle. refer to NSBundle for more information.

+4
source

In quick 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 the package name is the framework package identifier.

+1
source

Source: https://habr.com/ru/post/1385845/


All Articles