无法从iOS中的框架访问.nib(XIB)文件

时间:2011-12-12 07:54:39

标签: iphone ios xcode4 ios5

我已经从现有的代码库中创建了一个框架,并尝试在新的代码库中使用它。这很好。但是当我尝试访问作为我的框架包的一部分的nib文件时,我的应用程序正在崩溃。这是我用来访问视图控制器XIB文件的代码。

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

应用程序崩溃而不会生成任何错误或崩溃报告。这是我的问题

1 - 我们可以在框架中添加xib文件

2 - 如果,是的,从framawork添加和访问nib(xib)文件的正确方法是什么(目前我已将它们添加到“编译源构建设置中的”构建阶段“标签的部分”)

2 个答案:

答案 0 :(得分:4)

您需要编写nib所在的包的名称。文件是存储的,所以将上面的代码更改为...

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

这里你的bundle是NSBundle类型的对象。请参阅NSBundle课程以获取更多信息

答案 1 :(得分:1)

在swift 2中 - 对于故事板:

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)

对于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 故事板:

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

这里bundle name是framework的bundle id。