NSUnknownKeyException连接属性IB时发生错误。

huangapple go评论47阅读模式
英文:

NSUnknownKeyException when connecting property IB

问题

I am trying to integrate a UIKit view into my SwiftUI project using UIViewRepresentable as an intermediate.

A TestViewVC.swift file was created containing just a simple IBOutlet:

import Foundation
import UIKit

class TestViewVC: UIViewController {
    @IBOutlet weak var testLabel: UILabel!
}

A corresponding TestViewVC.xib file was creating containing just a single label centered on screen which was then connected to the testLabel IBOutlet. The TestViewVC was assigned as the File Owner's class.

These are then displayed within the SwiftUI context through the use of a TestRepresentableView.swift and TestViewVC.swift class (shown below).

// TestRepresentableView.swift

import SwiftUI

struct TestRepresentableView: UIViewRepresentable {
    func makeUIView(context: Context) -> UIView {
        // Load the XIB file and return its view
        let nib = UINib(nibName: "TestViewVC", bundle: nil)
        return nib.instantiate(withOwner: nil, options: nil)[0] as! UIView
    }
    
    func updateUIView(_ uiView: UIView, context: Context) {
        // Update the view here if needed
    }
}
// TestViewVC.swift

import SwiftUI

@main
struct TestApp: App {
    var body: some Scene {
        WindowGroup {
            TestRepresentableView()
        }
    }
}

The app runs fine on debug; however, an NSUnknownKeyException is raised whenever the label is connected to the IBOutlet (or any other outlet, even simply connecting the view results in the same error).

Any idea why this is happening?

英文:

I am trying to integrate a UIKit view into my SwiftUI project using UIViewRepresentable as an intermediate.

A TestViewVC.swift file was created containing just a simple IBOutlet:

import Foundation
import UIKit

class TestViewVC: UIViewController {
    @IBOutlet weak var testLabel: UILabel!
}

A corresponding TestViewVC.xib file was creating containing just a single label centred on screen which was then connected to the testLabel IBOoutlet. The TestViewVC wasassigned as the File Owner's class.

These are then displayed within the SwiftUI context through the use of a TestRepresentableView.swift and TestViewVC.swift class (shown below).

//TestRepresentableView.swift

import SwiftUI

struct TestRepresentableView: UIViewRepresentable {
    func makeUIView(context: Context) -> UIView {
        // Load the XIB file and return its view
        let nib = UINib(nibName: "TestViewVC", bundle: nil)
        return nib.instantiate(withOwner: nil, options: nil)[0] as! UIView
    }
    
    func updateUIView(_ uiView: UIView, context: Context) {
        // Update the view here if needed
    }
}
//TestViewVC.swift

import SwiftUI

@main
struct TestApp: App {
    var body: some Scene {
        WindowGroup {
            TestRepresentableView()
        }
    }
}

The app runs fine on debug however an NSUnknownKeyException is raised whenever label is connected to the IBOutlet. (Or any other outlet for that matter, even simply connecting the view results in the same error).

Any idea of which this is happening?

答案1

得分: 0

Geoff所指出,应该使用UIViewControllerRepresentable来代替UIViewRepresentable,以封装视图和视图控制器,包括任何需要的附加属性和行为。

更新后的代码如下:

import SwiftUI

struct TestRepresentableVC: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> TestViewVC {
        let viewController = TestViewVC(nibName: "TestViewVC", bundle: nil)
        return viewController
    }
    
    func updateUIViewController(_ uiViewController: TestViewVC, context: Context) {
        // 如果需要,可以在这里更新视图控制器
    }
}
英文:

As Geoff pointed out, a UIViewControllerRepresentable should be used instead of UIViewRepresentable to encapsulate both the view and the view controller, including any additional properties and behaviors that are needed.

Updated code below:

import SwiftUI

struct TestRepresentableVC: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> TestViewVC {
        let viewController = TestViewVC(nibName: "TestViewVC", bundle: nil)
        return viewController
    }
    
    func updateUIViewController(_ uiViewController: TestViewVC, context: Context) {
        // update the view controller here if needed
    }
}

huangapple
  • 本文由 发表于 2023年5月8日 01:37:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定