SwiftUI从视图传递参数

时间:2019-11-25 21:15:44

标签: swiftui xcode11.2

我有这种情况:

第一个视图,在这个视图中,我得到了当前位置,并将纬度和经度保存在两个双精度类型var中:doublelatdoublelng

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()
    }
}

现在只需按下按钮Button("Go to next page"),进入具有两个参数纬度和经度的视图AddressView(latitude: self.doubleLat, longitude: self.doubleLng)

这是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)
    } }

在第二个视图中,我必须传递此声明中前一个视图的两个参数(纬度和经度):

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

我该怎么办?

谢谢。

1 个答案:

答案 0 :(得分:1)

尝试对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)
    }
    ...