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

Iphone 无法从iOS中的框架访问.nib(XIB)文件,iphone,ios,xcode4,ios5,Iphone,Ios,Xcode4,Ios5,我已经用现有的代码库创建了一个框架,并尝试在新的代码库中使用它。这很有效。但是当我尝试访问nib文件时,我的应用程序崩溃了,nib文件是我的框架包的一部分。这是我用来访问视图控制器XIB文件的代码 testViewController*controller=[[testViewController alloc]initWithNibName:@“testViewController”bundle:nil]; [self.view addSubview:controller.view] 应用程序正

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


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

应用程序正在崩溃,但未生成任何错误或崩溃报告。以下是我的问题

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


2-如果是,从framawork添加和访问nib(xib)文件的正确方法是什么(目前我已在“构建设置”中的“构建阶段”选项卡的“编译源代码”部分添加了这些文件)

您需要写入nib所在的捆绑包的名称。文件已存储,请将上述代码更改为

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

这里的bundle是NSBundle类型的对象。有关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)
锡伯


这里bundle name是框架的bundle id。

testViewController*controller=[[testViewController alloc]initWithNibName:@“testViewController”bundle:@“FI”];[self.view addSubview:controller.view];我像这样修改了代码。这里FI是我新创建的bundle/framework的名称。但即使是现在,我的应用程序也会因为以下错误而崩溃。“[\uu NSCFConstantString pathForResource:of type:]:发送到实例0x6185c的选择器无法识别”。。。任何想法你不需要在这里给一个字符串。。看看苹果有什么要说的。。“-(id)initWithNibName:(NSString*)nibName bundle:(NSBundle*)nibBundle”它需要是NSBundle类型的对象。但是这里有一个陷阱,因为我的所有nib文件都在一个框架中。如何从捆绑包的角度引用它们?很抱歉,我之前误解了你的问题。据我所知,你不能将.xib添加到静态库,并且ios不支持个人框架,你可以在这里获得更多信息。。在justicle给出的第二个答案中,+1是一个好的链接。。。。我完全按照链接中的说明做了。。但是,还是没有运气。。坠机事件仍在继续…:(
let bundle = NSBundle(identifier:"com.bundileName.Name")
if !(bundle == nil){
    let objtestViewController = testViewController(nibName: "testViewController", bundle: bundle)
    present(objtestViewController, animated: true, completion: nil)
}