带有UIViewcontrollerRepresentable的自定义UIViewController,其UITextView在SwiftUI中被调用时崩溃或为零

问题描述 投票:0回答:1

我制作了一个自定义的UIViewController,称为ViewControllerA,并希望能够使用它,因此我制作了一个名为UIViewControllerRepresentableViewControllerARepresentable,如下所示,但问题是,当我在自己的计算机中调用ViewControllerARepresentable SwiftUI查看并传递stringToUpdateTextView的值,ViewControllerAhtmlTextView(UITextView)中的ViewControllerAnil,但我不确定为什么。

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,如下所示,问题是……

swiftui nsattributedstring uiviewcontrollerrepresentable
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.