【问题标题】:Custom UIViewController with UIViewcontrollerRepresentable that has a UITextView that crashes or is nil when called in SwiftUI具有 UIViewcontrollerRepresentable 的自定义 UIViewController,其 UITextView 在 SwiftUI 中调用时崩溃或为零
【发布时间】:2020-10-06 21:29:35
【问题描述】:

我已经制作了一个名为 ViewControllerA 的自定义 UIViewController 并希望能够使用它,所以我制作了一个名为 ViewControllerARepresentableUIViewControllerRepresentable ,如下所示,但问题是当我调用 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


    【解决方案1】:

    makeUIViewController 中实例化您的视图控制器时,出口尚未初始化。

    以下代码从情节提要加载您的视图控制器,并更新updateUIViewController 中的属性:

    ViewController.swift

    import UIKit
    import SwiftUI
    
    class ViewController: UIViewController {
    
      @IBOutlet weak var htmlTextView: UITextView!
    
      override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
      }
    
    }
    
    struct ViewControllerWrapper: UIViewControllerRepresentable {
      typealias UIViewControllerType = ViewController
    
      @Binding var text: String
    
      func makeUIViewController(context: Context) -> ViewController {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        guard let viewController =  storyboard.instantiateViewController(
          identifier: "ViewController") as? ViewController else {
            fatalError("Cannot load from storyboard")
        }
        return viewController
      }
    
      func updateUIViewController(_ uiViewController: ViewController, context: Context) {
        uiViewController.htmlTextView.text = text
      }
    
    }
    
    
    struct ViewControllerPreview: PreviewProvider {
      static var previews: some View {
        ViewControllerWrapper(text: .constant("hello world!"))
      }
    }
    

    SwiftUIView.swift

    struct SwiftUIView: View {
      @State var text = "Text"
      var body: some View {
        HStack {
          TextField("Text:", text: $text)
          ViewControllerWrapper(text: $text)
        }
      }
    }
    

    【讨论】:

    • 如果我想在 uitextview 下添加一个带有按钮(1-3)的小 uitableview 怎么办?我必须在 UIViewControllerRepresentable 中使用协调器吗?
    • 你能解释一下你想要达到的目标吗? IE。整个屏幕应该是什么样子?创建图纸/草图并将其添加到您的问题中可能会有所帮助。
    • 我想先有一个大的 uitextview 和转换后的 html 文本(完成),然后在 uitextview 下面有一个 tableview 我尝试在 UIViewControllerRepresentable 周围添加一个 ZStack 并在它下面放一个 List {} 但这很困难定位
    • 您是否考虑过使用UIViewRepresentable 代替?可能会使UITextView 的定位更容易。
    • 非常感谢这完美的作品!牙买加非常尊重!
    猜你喜欢
    • 2021-10-18
    • 2016-04-15
    • 1970-01-01
    • 2023-02-05
    • 1970-01-01
    • 2020-12-28
    • 2023-03-14
    • 1970-01-01
    • 2014-10-30
    相关资源
    最近更新 更多