gpt4 book ai didi

swift - 在这种 Firebase 情况下,如何在函数之间传递数据? swift + Xcode

转载 作者:行者123 更新时间:2023-11-30 11:20:47 27 4
gpt4 key购买 nike

我对 Swift 很陌生。我如何从 addPin 函数中检索注释并能够在我的 addLocation 操作 (buttonPressed) 中使用它。我正在尝试使用压力触摸在 map 上添加图钉,在两个单独的文本字段中输入名称和信息,然后将这四条信息添加到我的 Firebase 数据库中,可以在其中检索它们并将其绘制在另一张 map 上。与应用程序 Flush 的工作原理类似。

类AddLocationViewController:UIViewController {

@IBOutlet weak var nameTF : UITextField!
@IBOutlet weak var descriptionTF : UITextField!
@IBOutlet weak var longitudeTF : UITextField!
@IBOutlet weak var latitudeTF : UITextField!

@IBOutlet weak var addButton : UIButton!

@IBOutlet weak var readySwitch: UISwitch!


@IBAction func addLocation ( _ sender : Any? ) {

if let name = nameTF.text,
let info = descriptionTF.text,
let latitude = Optional(annotation.coordinate.latitude) ,
let longitude = Optional(annotation.coordinate.longitude),
name.isEmpty == false {

let dict : [String:Any] = ["name":name, "info":info, "latitude": latitude, "longitude":longitude]

let ref = Database.database().reference()

let newPointRef = ref.child("annotations").childByAutoId()
ref.child("annotations").child(newPointRef.key).setValue(dict)

}

}

@IBAction func textChanged ( _ sender : Any? ) {
validateUserInterface()
}


func validateUserInterface () {
addButton.isEnabled = false
guard let name = nameTF.text, name.isEmpty == false else { return

if let _ = CLLocationDegrees( latitudeTF.text ?? "" ),
let _ = CLLocationDegrees( longitudeTF.text ?? "" )
/*let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)*/ {
addButton.isEnabled = true
}
}

override func viewDidLoad() {
super.viewDidLoad()
validateUserInterface()
}

@IBOutlet weak var addCourtMapView: MKMapView!


@IBAction func addPin(_ sender: UILongPressGestureRecognizer) {

let location = sender.location(in: self.addCourtMapView)
let locCoord = self.addCourtMapView.convert(location, toCoordinateFrom: self.addCourtMapView)

let annotation = MKPointAnnotation()

annotation.coordinate = locCoord
annotation.title = "New Court"
annotation.subtitle = "The court is located here"
self.addCourtMapView.removeAnnotations(addCourtMapView.annotations)
self.addCourtMapView.addAnnotation(annotation)

}

}

最佳答案

您需要创建一个共享函数并像这样调用它

func sharedStore(name:String,info:String,longitude:Double,latitude:Double) {

if name.isEmpty == false {
let dict : [String:Any] = ["name":name, "info":info, "latitude": latitude, "longitude":longitude]
let ref = Database.database().reference()
let newPointRef = ref.child("annotations").childByAutoId()
ref.child("annotations").child(newPointRef.key).setValue(dict)

}
}

//

@IBAction func addLocation ( _ sender : Any? ) {

if let name = nameTF.text,
let info = descriptionTF.text,
let latitude = Optional(annotation.coordinate.latitude) ,
let longitude = Optional(annotation.coordinate.longitude),
name.isEmpty == false {
sharedStore(name:name, info:info, longitude: longitude, latitude: latitude)
}
}

//

@IBAction func addPin(_ sender: UILongPressGestureRecognizer) {

let location = sender.location(in: self.addCourtMapView)
let locCoord = self.addCourtMapView.convert(location, toCoordinateFrom: self.addCourtMapView)
let name = "New Court"
let info = "The court is located here"
let annotation = MKPointAnnotation()
annotation.coordinate = locCoord
annotation.title = name
annotation.subtitle = info
self.addCourtMapView.removeAnnotations(addCourtMapView.annotations)
self.addCourtMapView.addAnnotation(annotation)
sharedStore(name:name, info:info, longitude: locCoord.longitude, latitude :locCoord.latitude)
}

关于swift - 在这种 Firebase 情况下,如何在函数之间传递数据? swift + Xcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51286733/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com