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

标签 swift xcode firebase firebase-realtime-database mapkit

我对 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/

相关文章:

swift - 同一个 UIViewcontroller 中的两个选择器

Android Studio : App crashes after adding Firebase

ios - 无法访问自定义单元格中的标签

swift - 如何减少 Swift 中处理变量和 if 语句的代码?

ios tableview 表中的最后一个单元格缺少分隔符 swift

xcode - 如何在 Xcode 中制作数学方程式?

ios - Swift Codable 无法仅自定义所需的按键

iOS:在 numberOfRowsInSection 中返回 2 个计数值

firebase - 卡住的 Json 序列化程序 - firestore 通信

android - FCM 在被杀死时向 Chrome 推送通知