我做了一个自定义UIViewController
调用ViewControllerA
并希望能够使用它所以我做了一个如下所示的UIViewControllerRepresentable
调用 ,但问题是当我在我的 SwiftUI 视图中调用并传递一个值时,说in是,我是不知道为什么。ViewControllerARepresentable
ViewControllerARepresentable
stringToUpdateTextView
ViewControllerA
htmlTextView(UITextView)
ViewControllerA
nil
ViewControllerARepresentable(stringToUpdateTextView: "<html>Send some Html Text as string here</html>")
ViewControllerARepresentable
public struct ViewControllerARepresentable: UIViewControllerRepresentable {
var stringToUpdateTextView: String
public func makeUIViewController(context: Context) -> ViewControllerA {
let viewcontrollerA = ViewControllerA(testString: testingString)
return viewcontrollerA
}
public func updateUIViewController(_ uiViewController: ViewControllerA, context: Context) {}
}
视图控制器A
open class ViewControllerA: UIViewController {
public var stringToUpdateTextView: String
override open func viewDidLoad() {
super.viewDidLoad()
htmlTextView.text = stringToUpdateTextView
}
@IBOutlet weak var htmlTextView: UITextView!
public init(testString: String) {
self.testString = testString
super.init(nibName: nil, bundle: nil)
}
required public init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
在说 htmlTextView.text 为 nil 时发生崩溃, htmlTextView.text = stringToUpdateTextView
即使它是一个 IBOutlet。如果在 viewDidAppear 或 viewDidLoad 中调用,对 htmlTextView 所做的任何更改(如背景颜色等)也会导致崩溃