【问题标题】:unable to access .nib(XIB) files from a framework in iOS无法从 iOS 中的框架访问 .nib(XIB) 文件
【发布时间】:2012-01-18 06:30:11
【问题描述】:

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

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

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

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

2 - 如果是,那么从框架中添加和访问 nib(xib) 文件的正确方法是什么(目前我已将它们添加到“编译源代码 “构建设置”中我的“构建阶段”选项卡的“em>”部分)

【问题讨论】:

    标签: iphone ios xcode4 ios5


    【解决方案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)
        }
    

    斯威夫特 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)
    

    西布

    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。

    【讨论】:

      【解决方案2】:

      您需要写下您 nib 所在包的名称。文件已存储,因此将上面的代码更改为 ...

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

      这里你的bundle是一个NSBundle类型的对象。请参阅NSBundle 课程了解更多信息

      【讨论】:

      • testViewController *controller = [[testViewController alloc] initWithNibName:@"testViewController" bundle:@"FI"]; [self.view addSubview:controller.view];我修改了这个ankit的代码。这里 FI 是我新创建的包/框架的名称。但即使现在我的应用程序崩溃并出现以下错误。 “[__NSCFConstantString pathForResource:ofType:]: 无法识别的选择器发送到实例 0x6185c”...任何想法
      • 你不需要在这里给出一个字符串.. 看看苹果怎么说.. "- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle" 它需要是NSBundle 类型的对象。
      • 但是这里有一个问题 ankit,我所有的 nib 文件都在一个框架中。我如何从捆绑的角度引用它们?
      • 对不起,我之前误解了您的问题。据我所知,您不能将 .xib 添加到静态库,并且 ios 不支持个人框架,您可以在此处获取更多信息.. @987654322 @在justicle的第二个答案中。
      • +1 以获得良好的链接......我完全按照该链接中的说明做了......但是,仍然没有运气......崩溃仍然存在...... :(
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-25
      • 1970-01-01
      • 2012-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多