iOS:如何在框架内加载xib
我使用CocoaPod创建了一个包含xib视图的新框架,并创建了一个测试应用程序来测试该框架。测试应用程序包含一个按钮,用于调用该框架并显示xib 起初,我在框架内的这一行发生了崩溃:iOS:如何在框架内加载xib,ios,swift,cocoa-touch,xib,Ios,Swift,Cocoa Touch,Xib,我使用CocoaPod创建了一个包含xib视图的新框架,并创建了一个测试应用程序来测试该框架。测试应用程序包含一个按钮,用于调用该框架并显示xib 起初,我在框架内的这一行发生了崩溃: if let myView = Bundle.main.loadNibNamed("myCustomXib", owner: self, options: nil)?.first as? myCustomXibClass 日志:由于未捕获的异常“NSInternalInconsistencyException
if let myView = Bundle.main.loadNibNamed("myCustomXib", owner: self, options: nil)?.first as? myCustomXibClass
日志:由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法在名为“myCustomXib”的捆绑包中加载NIB:“NSBundle(loaded)”中
然后我用这个词改变了这句话:
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"]
我把所有的文件都放在我的本地开发舱里。故事板工作得很好,但xib不行。那么我该怎么做呢?创建一个简短的静态类,我们称之为MyFrameworkBundle: 对于WindowController中的XIB:
如果您从“developmentpods”目录本地运行代码,请在第一次运行之前使用
CMD+SHIFT+K
。有人遇到过这个问题吗?您好,您得到了上述解决方案。听起来您是从“developmentpods”目录本地运行代码的,如果是这样,请使用CMD+SHIFT+K生效
public final class MyFrameworkBundle {
public static let main: Bundle = Bundle(for: MyFrameworkBundle.self)
}
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
}
}