ios - UIViewControllerRepresentable 运行时错误 : Nib is loaded but view outlet wasn't set. 为什么?

标签 ios swiftui

我正在玩弄 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/

相关文章:

ios - 使用重置解析功能解析 iOS

ios - 从推送通知打开 View Controller 时如何管理 View 层次结构

ios - 如何将 ObservableObject 与 UIViewRepresentable 一起使用

iOS SwiftUI : ScrollView ignore top safe area

swiftui - 将数组传递给另一个 View SwiftUI

ios - 使表格 View 单元格在 Swift 中完全填满屏幕

iOS 7 自定义过渡故障

ios - 从 AFNetworking 下载设置 TableView 单元格图像

swift - 将按钮放置在节页眉/页脚上时,iOS 14 上出现奇怪的 UI 渲染问题

ios - SpriteView 不会在状态更改时暂停场景