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

时间:2018-07-11 13:28:46

标签: swift xcode firebase firebase-realtime-database mapkit

我对Swift非常陌生。如何从addPin函数检索注释,并能够在addLocation操作(buttonPressed)中使用它。我正在尝试使用强制触摸在地图上添加图钉,在两个单独的文本字段中输入名称和信息,然后将这四个信息添加到我的Firebase数据库中,以便在其中检索和绘制它们。类似于应用程序“刷新”的工作方式。

类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)

 }

}

2 个答案:

答案 0 :(得分:0)

您需要创建一个共享的func并这样调用

mx-auto

//

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

答案 1 :(得分:0)

您可以使用addCourtMapView.annotations在MapView上获取注释:

@IBAction func addLocation ( _ sender : Any? ) {
    guard let annotation = addCourtMapView.annotations.first else {
        return
    }

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