Я тестировал SwiftUI , чтобы увидеть, как далеко я могу go с ним. Теперь я хочу выяснить, могу ли я передать строку из 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
? Спасибо.