我正在玩弄 SwiftUI 通过 UIViewControllerRepresentable 协议(protocol)对 UIViewController 的访问;使用 Nib 与手动实例化类。
我见过的大多数演示示例都在代码中实例化了 ViewController。如下所示:
struct RicViewVC: UIViewControllerRepresentable {
// Parameters ----------------------------
// let url: URL
@Binding var showingVC: Bool
// ---------------------------------------
// 1)
func makeUIViewController(context _: Context) -> RicViewController {
let viewController = RicViewController()
Bundle.main.loadNibNamed("RicView", owner: viewController, options: nil)
return viewController
}
// 2)
func updateUIViewController(_: RicViewController, context _: Context) {}
// 3
func makeCoordinator() -> RicViewVC.Coordinator {
Coordinator(showing: $showingVC)
}
class Coordinator: NSObject {
@Binding var showingCoord: Bool
init(showing: Binding<Bool>) {
_showingCoord = showing
}
}
}
但是,当 UIViewController 与 IB 链接时,这不起作用:
Hello from RicViewController 2020-02-23 10:53:55.893551-0800
SafariRepresentable[9792:151550] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "RicView" nib but the view outlet was not set.'
这就是我感到困惑的地方。
如何将 IB 的实例化 XIB 链接到 #1(参见上面的代码):makeUIViewController()?
语法是什么?
最佳答案
如果您的 RicViewController
是 XIB 所有者,然后尝试以下操作
let viewController = RicViewController(nibName: "_nib_name_here", bundle: nil) // if bundle not main the specify also bundle
return viewController
关于ios - UIViewControllerRepresentable 运行时错误 : Nib is loaded but view outlet wasn't set. 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60365853/