[英]Change SwiftUI Text from UIViewController
I'm quite new to Swift and absolute new to SwiftUI.我对 Swift 很陌生,对 SwiftUI 绝对陌生。 I'm trying to combine
UIViewController
Google Maps and modern SwiftUI.我正在尝试将
UIViewController
Google Maps 和现代 SwiftUI 结合起来。 In SwiftUI i have a few Text
objects, and I want my GmapsController
class to be able to modify these Text values, and redraw SwiftUI struct.在 SwiftUI 中,我有一些
Text
对象,我希望我的GmapsController
类能够修改这些 Text 值,并重绘 SwiftUI 结构。
My main struct:我的主要结构:
var _swiftUIStruct : swiftUIStruct = swiftUIStruct() // to have access in GmapsController
struct GoogMapView: View {
var body: some View {
let gmap = GmapsControllerRepresentable()
return ZStack {
gmap
_swiftUIStruct
}
}
}
UIViewControllerRepresentable
wrapper of GmapsController
: GmapsController
UIViewControllerRepresentable
包装器:
struct GmapsControllerRepresentable: UIViewControllerRepresentable {
func makeUIViewController(context: UIViewControllerRepresentableContext<GmapsControllerRepresentable>) -> GmapsController {
return GmapsController()
}
func updateUIViewController(_ uiViewController: GmapsController, context: UIViewControllerRepresentableContext<GmapsControllerRepresentable>) {}
}
The GmapsController
itself: GmapsController
本身:
class GmapsController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate {
var locationManager = CLLocationManager()
var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
locationManager.requestWhenInUseAuthorization()
locationManager.delegate = self
let camera = GMSCameraPosition.camera(
withLatitude: (locationManager.location?.coordinate.latitude)!,
longitude: (locationManager.location?.coordinate.longitude)!,
zoom: 15
)
mapView = GMSMapView.map(withFrame: view.bounds, camera: camera)
mapView.delegate = self
self.view = mapView
}
// HERE I WANT TO CHANGE SOME swiftUIStruct Text FIELDS
// calls after map move
func mapView(_ mapView: GMSMapView, idleAt сameraPosition: GMSCameraPosition) {
_swiftUIStruct.someTxt = "I hope this will work" // compilation pass, but value doesn't change
}
}
And the swiftUIStruct struct:和 swiftUIStruct 结构:
struct swiftUIStruct {
@State var someTxt = ""
var body: some View {
Text(someTxt) // THE TEXT I WISH I COULD CHANGE
}
}
Googling this a whole day just made me feel dumb, any help is appreciated.在谷歌上搜索了一整天让我觉得很愚蠢,任何帮助表示赞赏。
I hope my example code helps.我希望我的示例代码有所帮助。 Basically, move the model data outside, and pass it along, and change it.
基本上,将模型数据移到外面,并传递它,然后更改它。 If you run this code, you will see the text "I hope this will work", NOT "Initial Content".
如果您运行此代码,您将看到文本“我希望这会起作用”,而不是“初始内容”。
import SwiftUI
class ViewModel: ObservableObject {
@Published var someTxt = "Initial Content"
}
struct ContentView: View {
@ObservedObject var viewModel = ViewModel()
var body: some View {
ZStack {
GoogleMapsView(viewModel: viewModel)
Text(viewModel.someTxt)
}
}
}
struct GoogleMapsView: UIViewControllerRepresentable {
var viewModel: ViewModel
func makeUIViewController(context: Context) -> GmapsController {
let controller = GmapsController()
controller.viewModel = viewModel
return controller
}
func updateUIViewController(_ uiViewController: GmapsController, context: Context) {}
}
class GmapsController: UIViewController {
var viewModel: ViewModel?
override func viewDidLoad() {
viewModel?.someTxt = "I hope this will work"
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
return ContentView()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.