SwiftUI 从视图传递参数
SwiftUI pass param from views
我遇到这种情况:
第一个视图,在这个视图中,我得到了当前位置,并将纬度和经度保存在两个双精度类型变量中:doublelat
和 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()
}
}
现在只需按下按钮 Button("Go to next page")
,转到视图 AddressView(latitude: self.doubleLat, longitude: self.doubleLng)
,其中包含两个参数纬度和经度。
这是地址视图:
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>)
我该怎么办?
谢谢。
为 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)
}
...
我遇到这种情况:
第一个视图,在这个视图中,我得到了当前位置,并将纬度和经度保存在两个双精度类型变量中:doublelat
和 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()
}
}
现在只需按下按钮 Button("Go to next page")
,转到视图 AddressView(latitude: self.doubleLat, longitude: self.doubleLng)
,其中包含两个参数纬度和经度。
这是地址视图:
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>)
我该怎么办?
谢谢。
为 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)
}
...