Search code examples
swiftuixcode11

SwiftUI pass param from views


I have this situation:

First View, in this view i got the current position and i save the latitude and longitude in two double type var: doublelat and doublelng

import SwiftUI

struct CoordinateView: View {
    
    @ObservedObject var locationManager = LocationManager()
    
    @State private var gotopage = false
    
    var userLatitude: String {
        let lat = String(format: "%.6f", locationManager.lastLocation?.coordinate.latitude ?? 0)
        return "\(lat)"
    }
    
    var userLongitude: String {
        let lng = String(format: "%.6f", locationManager.lastLocation?.coordinate.longitude ?? 0)
        return "\(lng)"
    }
    
    var doubleLat : Double {
        return locationManager.lastLocation?.coordinate.latitude ?? 0
    }
    
    var doubleLng : Double {
        return locationManager.lastLocation?.coordinate.longitude ?? 0
    }
    
    private var getcoordinates: Bool {
        if locationManager.statusCode == 0 {
            return false
        } else {
            return true
        }
        
    }
    
    var body: some View {
        VStack {
            HStack() {
                Text("Your position")
                    .font(.headline)
                Spacer()
            }
            
            VStack(alignment: .leading) {
                HStack(){
                    Text("Lat")
                    Spacer()
                    Text("\(userLatitude)")
                }.padding(5)
                
                HStack(){
                    Text("Long")
                    Spacer()
                    Text("\(userLongitude)")
                }.padding(5)
                
            }
            
            if getcoordinates {
                HStack(alignment: .center){
                    Button("Go to next page") {
                        self.gotopage = true
                    }
                    
                }.padding()
                
                VStack {
                    if self.gotopage {
                        AddressView(latitude: self.doubleLat, longitude: self.doubleLng)
                    }
                }
            }
        }
        .padding()
    }
};

struct CoordinateView_Previews: PreviewProvider {
    static var previews: some View {
        CoordinateView()
    }
}

Now just press on button Button("Go to next page"), go to view AddressView(latitude: self.doubleLat, longitude: self.doubleLng), with two param latitude and longitude.

This is AddressView:

import SwiftUI

struct AddressView: View {
    
    var latitude: Double
    var longitude: Double
    
    
    @ObservedObject var apiManager = APIManager(lat: <latitude>, lng: <longitude>)
    
    var body: some View {
        
            .....
            .....
    
    }

struct AddressView_Previews: PreviewProvider {
    static var previews: some View {
        AddressView(latitude: 14.564378, longitude: 42.674532)
    } }

In this second view i have to pass the two param (latitude and longitude) from previous view in this declaration:

@ObservedObject var apiManager = APIManager(lat: <latitude>, lng: <longitude>)

How can I do?


Solution

  • Try the following declaration for AddressView

    struct AddressView: View {
    
        var latitude: Double
        var longitude: Double
        @ObservedObject var apiManager: APIManager
    
        init(latitude: Double, longitude: Double) {
            self.latitude = latitude
            self.longitude = longitude
            self.apiManager = APIManager(lat: latitude, lng: longitude)
        }
        ...