我制作了一个自定义的UIViewController
,称为ViewControllerA
,并希望能够使用它,因此我制作了一个名为UIViewControllerRepresentable
的ViewControllerARepresentable
,如下所示,但问题是,当我在自己的计算机中调用ViewControllerARepresentable
SwiftUI查看并传递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) {}
}
ViewControllerA
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 = stringToUpdateTextView
,表示htmlTextView.text为零,即使它是IBOutlet也是如此。如果在viewDidAppear或viewDidLoad中调用,则对htmlTextView所做的任何更改(例如背景色等)也会导致崩溃。
我制作了一个名为ViewControllerA的自定义UIViewController,并希望能够使用它,所以我制作了一个名为ViewControllerARepresentable的UIViewControllerRepresentable,如下所示,问题是……