将数据从 SwiftUI 视图传递到 UIViewController
Passing Data from SwiftUI View to UIViewController
我一直在测试 SwiftUI 看看我能用它走多远。现在,我想知道是否可以将字符串从 SwiftUI View
直接传递到 UIViewController
。我想用 UILabel
显示这个字符串。 UILabel
是我故事板上的所有内容。
下面是我的视图控制器(UIViewController
)。
import UIKit
import SwiftUI
class HomeViewController: UIViewController {
var message = String()
@IBOutlet weak var messageLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
messageLabel.text = message
}
}
struct HomeViewControllerRepresentation: UIViewControllerRepresentable {
func makeUIViewController(context: UIViewControllerRepresentableContext<HomeViewControllerRepresentation>) -> HomeViewController {
UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "homeViewController") as! HomeViewController
}
func updateUIViewController(_ uiViewController: HomeViewController, context: UIViewControllerRepresentableContext<HomeViewControllerRepresentation>) {
}
}
我的 SwiftUI View
如下。
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: HomeViewControllerRepresentation(message: "GGG")) { // Error
Text("Tap me")
}
}
.navigationBarTitle("")
.navigationBarHidden(true)
}
}
}
我希望我可以通过 HomeViewControllerRepresentation 传递一个字符串。但这是行不通的。有没有一种简单的方法可以将数据从 SwiftUI View
传递到 UIViewController
?谢谢。
UIViewControllerRepresentable
不是代理,而是包装器,所以一切都应该手动传输,比如
struct HomeViewControllerRepresentation: UIViewControllerRepresentable {
var message: String
func makeUIViewController(context: UIViewControllerRepresentableContext<HomeViewControllerRepresentation>) -> HomeViewController {
let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "homeViewController") as! HomeViewController
controller.message = self.message
return controller
}
func updateUIViewController(_ uiViewController: HomeViewController, context: UIViewControllerRepresentableContext<HomeViewControllerRepresentation>) {
}
}
我一直在测试 SwiftUI 看看我能用它走多远。现在,我想知道是否可以将字符串从 SwiftUI View
直接传递到 UIViewController
。我想用 UILabel
显示这个字符串。 UILabel
是我故事板上的所有内容。
下面是我的视图控制器(UIViewController
)。
import UIKit
import SwiftUI
class HomeViewController: UIViewController {
var message = String()
@IBOutlet weak var messageLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
messageLabel.text = message
}
}
struct HomeViewControllerRepresentation: UIViewControllerRepresentable {
func makeUIViewController(context: UIViewControllerRepresentableContext<HomeViewControllerRepresentation>) -> HomeViewController {
UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "homeViewController") as! HomeViewController
}
func updateUIViewController(_ uiViewController: HomeViewController, context: UIViewControllerRepresentableContext<HomeViewControllerRepresentation>) {
}
}
我的 SwiftUI View
如下。
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: HomeViewControllerRepresentation(message: "GGG")) { // Error
Text("Tap me")
}
}
.navigationBarTitle("")
.navigationBarHidden(true)
}
}
}
我希望我可以通过 HomeViewControllerRepresentation 传递一个字符串。但这是行不通的。有没有一种简单的方法可以将数据从 SwiftUI View
传递到 UIViewController
?谢谢。
UIViewControllerRepresentable
不是代理,而是包装器,所以一切都应该手动传输,比如
struct HomeViewControllerRepresentation: UIViewControllerRepresentable {
var message: String
func makeUIViewController(context: UIViewControllerRepresentableContext<HomeViewControllerRepresentation>) -> HomeViewController {
let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "homeViewController") as! HomeViewController
controller.message = self.message
return controller
}
func updateUIViewController(_ uiViewController: HomeViewController, context: UIViewControllerRepresentableContext<HomeViewControllerRepresentation>) {
}
}