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

具有UIViewcontrollerRepresentable的自定义UIViewController,其UITextView在SwiftUI中调用时崩溃或为零,swiftui,nsattributedstring,uiviewcontrollerrepresentable,Swiftui,Nsattributedstring,Uiviewcontrollerrepresentable,我制作了一个名为ViewControllerA的自定义UIViewController并希望能够使用它,因此我制作了一个名为viewcontrollerrepresentable的UIViewControllerRepresentable,如下所示,但问题是,当我在SwiftUI视图中调用ViewControllerRepresentable并为stringToUpdateTextView传递一个值时,ViewControllerA表示ViewControllerA中的htmlTextView(

我制作了一个名为
ViewControllerA
的自定义
UIViewController
并希望能够使用它,因此我制作了一个名为
viewcontrollerrepresentable
UIViewControllerRepresentable
,如下所示,但问题是,当我在SwiftUI视图中调用
ViewControllerRepresentable
并为
stringToUpdateTextView
传递一个值时,
ViewControllerA
表示
ViewControllerA
中的
htmlTextView(UITextView)
nil
,我不知道为什么

ViewControllerRepresentable(stringToUpdateTextView:“在此处以字符串形式发送一些Html文本”)
可视控制器可表示

公共结构ViewControllerRepresentable:UIViewControllerRepresentable{ var stringToUpdateTextView:字符串 public func makeUIViewController(上下文:context)->ViewControllerA{ 让viewcontrollerA=viewcontrollerA(testString:testingString) 返回视图控制器 } public func updateUIViewController(uiViewController:ViewControllerA,context:context){} } 视图控制器

开放类ViewControllerA:UIViewController{
公共变量stringToUpdateTextView:String
覆盖打开的func viewDidLoad(){
super.viewDidLoad()
htmlTextView.text=stringToUpdateTextView
}
@IBOutlet弱var htmlTextView:UITextView!
公共初始化(testString:String){
self.testString=testString
super.init(nibName:nil,bundle:nil)
}
必需的公共初始化?(编码者:NSCoder){
fatalError(“初始化(编码者:)尚未实现”)
}
}
崩溃发生在
htmlTextView.text=stringToUpdateTextView
上,表示htmlTextView.text为零,即使它是一个IBOutlet。
如果在ViewDidDisplay或viewDidLoad中调用,对htmlTextView所做的任何更改(如背景色等)也会导致崩溃。在
makeUIViewController
中实例化视图控制器时,插座尚未初始化

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

ViewController.swift

导入UIKit
导入快捷键
类ViewController:UIViewController{
@IBOutlet弱var htmlTextView:UITextView!
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后执行任何其他设置。
}
}
结构ViewControllerRapper:UIViewControllerRepresentable{
typealias UIViewControllerType=ViewController
@绑定变量文本:字符串
func makeUIViewController(上下文:context)->ViewController{
let storyboard=UIStoryboard(名称:“Main”,捆绑包:nil)
guard let viewController=storyboard.InstanceEviewController(
标识符:“ViewController”)作为?ViewController else{
fatalError(“无法从情节提要加载”)
}
返回视图控制器
}
func updateUIViewController(uUIViewController:ViewController,上下文:上下文){
uiViewController.htmlTextView.text=文本
}
}
结构ViewControllerPreview:PreviewProvider{
静态var预览:一些视图{
ViewControllerRapper(文本:。常量(“hello world!”)
}
}
SwiftUIView.swift

struct SwiftUIView:视图{
@State var text=“text”
var body:一些观点{
HStack{
文本字段(“文本:”,文本:$Text)
ViewControllerRapper(文本:$text)
}
}
}

makeUIViewController
中实例化视图控制器时,插座尚未初始化

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

ViewController.swift

导入UIKit
导入快捷键
类ViewController:UIViewController{
@IBOutlet弱var htmlTextView:UITextView!
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后执行任何其他设置。
}
}
结构ViewControllerRapper:UIViewControllerRepresentable{
typealias UIViewControllerType=ViewController
@绑定变量文本:字符串
func makeUIViewController(上下文:context)->ViewController{
let storyboard=UIStoryboard(名称:“Main”,捆绑包:nil)
guard let viewController=storyboard.InstanceEviewController(
标识符:“ViewController”)作为?ViewController else{
fatalError(“无法从情节提要加载”)
}
返回视图控制器
}
func updateUIViewController(uUIViewController:ViewController,上下文:上下文){
uiViewController.htmlTextView.text=文本
}
}
结构ViewControllerPreview:PreviewProvider{
静态var预览:一些视图{
ViewControllerRapper(文本:。常量(“hello world!”)
}
}
SwiftUIView.swift

struct SwiftUIView:视图{
@State var text=“text”
var body:一些观点{
HStack{
文本字段(“文本:”,文本:$Text)
ViewControllerRapper(文本:$text)
}
}
}

如果我想在uitextview下添加一个带有按钮(1-3)的小型uitableview,该怎么办?我是否必须在UIViewControllerRepresentable中使用协调员?您能解释一下您想要实现的目标吗?也就是说,整个屏幕应该是什么样子?创建一个绘图/草图并将其添加到您的问题中可能会有所帮助。我想先创建一个带有转换html文本(完成)的大型uitextview,然后在uitextview下面创建一个表视图。我尝试在UIViewControllerRepresentable周围添加一个ZStack,并放置一个列表{}但这很难定位。您是否考虑过改用
UIViewRepresentable
?可能会使
UITextView
的定位更容易。非常感谢,这非常好用!从牙买加赢得很多尊敬!如果我想在uitextview下添加一个带有按钮(1-3)的小型uitableview,该怎么办?我必须在UIViewControllerRepresentable中使用协调器吗?可以吗