【发布时间】:2021-01-31 19:51:06
【问题描述】:
我找不到解释如何将 UIViewController 拥有的变量(String 或 Int)的值传递给调用视图的 SwiftUI 视图的方法或好的教程。
例如:
class ViewController: UIViewController {
var myString : String = "" // variable of interest
....
func methodThatChangeValueOfString(){
myString = someValue
}
}
// to make the view callable on SwiftUI
extension ViewController: UIViewControllerRepresentable {
typealias UIViewControllerType = ViewController
public func makeUIViewController(context: UIViewControllerRepresentableContext<ViewController>) -> ViewController {
return ViewController()
}
func updateUIViewController(_ uiViewController: ViewController, context: UIViewControllerRepresentableContext<ViewController>) {
}
}
在 SwiftUI 中我将拥有
struct ContentView: View {
var body: some View {
ViewController()
}
}
如何获取 ViewController 的 myString 并在 ContentView 中使用它? 提前致谢
【问题讨论】:
-
有几种方法,但目标不是很明确。
-
好的,我会解释我的具体问题。在 ViewController 中,我有摄像头,并使用 Vision 进行对象检测。相机检测到的对象的名称作为字符串保存在变量中。我想使用 SwiftUI 在屏幕上打印字符串,但我无法将数据传递给我的 SwiftUI 视图。
标签: ios data-binding uiviewcontroller swiftui uikit