unable to access .nib(XIB) files from a framework

2019-05-07 21:09发布

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条回答
够拽才男人
2楼-- · 2019-05-07 21:31

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

查看更多
闹够了就滚
3楼-- · 2019-05-07 21:31

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.

查看更多
登录 后发表回答