【问题标题】:iOS: How to load a xib inside frameworkiOS:如何在框架内加载 xib
【发布时间】:2018-08-16 15:09:44
【问题描述】:

我创建了一个包含使用 CocoaPod 的 xib 视图的新框架,以及一个用于测试该框架的测试应用程序。测试应用程序包含一个调用该框架并显示 xib 的按钮。

起初我在这一行的框架内发生了崩溃:

if let myView = Bundle.main.loadNibNamed("myCustomXib", owner: self, options: nil)?.first as? myCustomXibClass 

日志:Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/User/Library/Developer/CoreSimulator/Devices/F7B870D5-4CEC-4CC7-9C76-75DF1408A26C/data/Containers/Bundle/Application/20E3D273-315C-43EE-95CC-9B00B8EE8B6E/appDemo.app> (loaded)' with name 'myCustomXib''

然后我用这个改变了那行:

let bundle = Bundle(identifier:"com.framework.identifier")

    if let myView = bundle?.loadNibNamed("myCustomXib", owner: self, options: nil)?.first as? myCustomXibClass {

崩溃消失了,但没有显示任何内容。

这是我在 podspec 中添加源代码的方式:

  s.resources   = ["**/*.{storyboard,xib}", "Assets.xcassets"]

我得到了本地开发 pod 中的所有文件。故事板完美地工作,但不是 xib。那我该怎么办?

【问题讨论】:

  • 有人遇到过这个问题吗?
  • 嗨,你得到了上述解决方案。
  • 听起来你是从“Development Pods”目录本地运行你的代码,如果是这样使用CMD+SHIFT+K生效

标签: ios swift cocoa-touch xib


【解决方案1】:

创建一个简短的静态类,我们称之为MyFrameworkBundle

public final class MyFrameworkBundle {
    public static let main: Bundle = Bundle(for: MyFrameworkBundle.self)
}

对于 WindowController 中的 XIB

public class MyFrameworkWindowController: NSWindowController {

    override public var windowNibName: String! {
        return "MyFrameworkWindowController"
    }

    public init() {
        super.init(window: nil) 

        /* Load window from xib file */
        MyFrameworkBundle.main.loadNibNamed(windowNibName, owner: self, topLevelObjects: nil)
    }

    // Override this as required per the class spec
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented. Use init()")
    }

    override public func awakeFromNib() {
        //Do your stuff
    }
}

如果您从“Development Pods”目录在本地运行代码,请在首次运行之前使用CMD+SHIFT+K

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-04
    • 2022-01-02
    • 2023-03-16
    • 2014-05-01
    • 2019-10-01
    • 1970-01-01
    • 2018-01-27
    • 2014-02-13
    相关资源
    最近更新 更多