【发布时间】:2020-10-06 21:29:35
【问题描述】:
我已经制作了一个名为 ViewControllerA 的自定义 UIViewController 并希望能够使用它,所以我制作了一个名为 ViewControllerARepresentable 的 UIViewControllerRepresentable ,如下所示,但问题是当我调用 ViewControllerARepresentable 时我的 SwiftUI 视图并为stringToUpdateTextView 传递了一个值,ViewControllerA 表示ViewControllerA 中的htmlTextView(UITextView) 是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 为 nil,即使它是一个 IBOutlet。
如果在 viewDidAppear 或 viewDidLoad 中调用,对 htmlTextView 所做的任何更改(如背景颜色等)也会导致崩溃
【问题讨论】:
标签: swiftui nsattributedstring uiviewcontrollerrepresentable