Hello, I'm trying to pass a data from an observableObject to a fullScreenCover View, but whenever value changes nothing appears to be changed in the View (ListsSearchView).
I'm declaring the fullScreenCover to present a View like this
.fullScreenCover(isPresented: self.$showView, onDismiss: OnDismiss) {
ListsSearchView(startingPoint: self.$startingPoint, endingPoint: self.$endingPoint, addressType: self.$addressType, source: self.$source, destination: self.$destination, showView: self.$showView)
and the code for the View is:
import SwiftUI
import CoreLocation
struct ListsSearchView: View {
@Environment(\.colorScheme) var colorScheme
@Binding var startingPoint: String
@Binding var endingPoint: String
@Binding var addressType: addressType
@Binding var source: CLLocationCoordinate2D!
@Binding var destination: CLLocationCoordinate2D!
@Binding var showView: Bool
@StateObject var manager = locationManager()
var body: some View {
NavigationView {
VStack {
HStack {
Button {
OnDismiss()
} label: {
Image(systemName: "arrow.left")
.resizable()
.frame(width: 18, height: 17)
.font(.title)
.foregroundColor(.black)
.padding(6)
.background(Color.white)
.clipShape(Circle())
}
Spacer()
Text("\(manager.fetchedPlaces?.count ?? 0)")
}.padding(.horizontal, 10)
SearchView(startingPoint: $startingPoint, endingPoint: $endingPoint, addressType: $addressType, showView: $showView)
if let places = self.manager.fetchedPlaces, !places.isEmpty {
List{
ForEach(places, id: \.self){ place in
HStack(spacing: 15){
Image(systemName: "mapping.circle.fill")
.font(.title2)
.foregroundColor(.gray)
VStack(){
Text(place.name ?? "")
.font(.title3.bold())
Text(place.locality ?? "")
.font(.caption)
.foregroundColor(.gray)
}
}
}
}
.listStyle(.plain)
}
else{
List{
Button {
//CODE action
switch addressType {
case .start:
self.startingPoint = manager.getAddressReadablelocation(for: (manager.manager.location!.coordinate))
//TODO: remove the force unwrapping from both source and destination....
source = manager.manager.location?.coordinate ?? CLLocationCoordinate2D(latitude: 0.00, longitude: 0.00)
print(startingPoint)
case .end:
self.endingPoint = manager.getAddressReadablelocation(for: (manager.manager.location!.coordinate))
destination = manager.manager.location?.coordinate ?? CLLocationCoordinate2D(latitude: 0.00, longitude: 0.00)
print(endingPoint)
}
} label: {
HStack {
Image(systemName: "mappin")
Text("Use current location").foregroundColor(Color.primary)
}
}
NavigationLink(
destination: DropPinView(addressType: self.$addressType, startingPoint: self.$startingPoint, endingPoint: self.$endingPoint, source: self.$source, destination: self.$destination)
,label: {
Image(systemName: "map")
Text("use the map")
.foregroundColor(colorScheme == .dark ? Color.white : Color.black)
}
)
Section(header: Text("Suggestions")){
}
}
}
}
.navigationBarHidden(true)
.onChange(of: self.manager.SearchText) { _ in
manager.updatingSearchText()
print("\(manager.fetchedPlaces?.count ?? 0)")
}
}.environmentObject(manager)
}
func OnDismiss()-> (){
DispatchQueue.main.async {
self.showView = false
}
}
}
struct ListsSearchView_Previews: PreviewProvider {
static var previews: some View {
ListsSearchView(startingPoint: .constant("My location"), endingPoint: .constant(""), addressType: .constant(.end), source: .constant(CLLocationCoordinate2D(latitude: 33.99, longitude: 33.99)), destination: .constant(CLLocationCoordinate2D(latitude: 33.99, longitude: 33.99)), showView: .constant(true))
}
}
the value that I'm expecting it to be changed is from the stateObject manager which is manager.fetchedPlaces (it is an optional array of CLPlaceMark)
class locationManager: NSObject, CLLocationManagerDelegate, ObservableObject, MKMapViewDelegate{
@Published var region = MKCoordinateRegion()
@Published var manager : CLLocationManager = .init()
@Published var mapView: MKMapView = .init()
@Published var SearchText: String = ""
@Published var fetchedPlaces: [CLPlacemark]?
Post
Replies
Boosts
Views
Activity
Hi, I have an issue in xcode 13,when i implement two CLGeocoder.reverseGeocodeLocation(), i get error that says Segnentation fault: 11, any help?
thanks.
the code:
` CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: self.parent.source.latitude, longitude: self.parent.source.longitude)){ (placemarks, error) in
guard error == nil else{
print((error?.localizedDescription)!)
return
}
if (placemarks?.count)! > 0 {
let pm = placemarks?[0] as! CLPlacemark!
self.parent.userLocation = (pm!.subThoroughfare)! + "" + (pm?.thoroughfare)! + "" + (pm?.locality)!
}
}
}
}