使用不同的视图控制器删除地图上的数据

时间:2017-06-02 19:45:13

标签: swift firebase uiviewcontroller annotations modal-dialog

我真的在一个我认为相当有趣且非常困难的问题上苦苦挣扎。我的应用程序允许用户在Mapview中创建注释位置。他们还可以选择在另一个模态视图控制器中编辑和删除这些位置。

我面临的问题是,当用户按下删除时,会从firebase中删除该位置,注释仍会显示在地图上。我无法在视图中重新加载我的注释数据,因为这不适合我的应用程序。每次调出Mapview时,我都无法重新加载注释。

我需要找出一种在按下删除按钮时实现注释重新加载的方法。但是,由于这发生在我的删除视图控制器(不包含mapView)中,我无法使用重载功能。有没有办法连接视图控制器,以便我可以在按下删除时应用重新加载功能?

更新代码**

这是我的地图视图控制器:

class ViewController: UIViewController, SideBarDelegate, MGLMapViewDelegate, DeleteVCDelegate {
let EditSaveSpotController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "EditVC") as! EditSaveSpotViewController

 override func viewDidLoad() {
    super.viewDidLoad()


    EditSaveSpotController.delegate = self

}


func wholeRefresh() {

    let uid = FIRAuth.auth()!.currentUser!.uid

    let userLocationsRef = FIRDatabase.database().reference(withPath: "users/\(uid)/personalLocations")

    userLocationsRef.observe(.value, with: { snapshot in


        for item in snapshot.children {
            guard let snapshot = item as? FIRDataSnapshot else { continue }

            let newSkatepark = Skatepark(snapshot: snapshot)

            self.skateparks.append(newSkatepark)

            self.addAnnotation(park: newSkatepark)


        }
    })

    if let annotations = mapView.annotations {

        mapView.removeAnnotations(annotations)

    }

    for item in skateparks {

        self.addAnnotation(park: item)

    }



}

这是我的删除视图控制器:

 import UIKit
 import Firebase

 protocol DeleteVCDelegate {
 func wholeRefresh()
 }

 class EditSaveSpotViewController: UIViewController  {
 var delegate: DeleteVCDelegate?


  @IBAction func deleteSkateSpot(_ sender: Any) {




    ref = FIRDatabase.database().reference(withPath: "users").child(Api.User.CURRENT_USER!.uid).child("personalLocations/\(parkId!)")

    ref.observe(.value, with: { (snapshot) in



        self.ref.setValue(nil)

        self.dismiss(animated: true, completion: nil)

        self.delegate?.wholeRefresh()

     //   self.delegate?.mainRefresh()

        print("CheckWorking")


    })



}

}

1 个答案:

答案 0 :(得分:0)

这是非常高的水平,我没有机会验证,但它应该足以让你去:

模态删除视图

protocol DeleteVCDelegate {
    func mainRefresh()
}

class DeleteVC: UIViewController {
    var delegate: DeleteVCDelegate?

    //your delete code
    @IBAction func deleteSkateSpot(_ sender: Any) {
        ref = FIRDatabase.database().reference(withPath: "users").child(Api.User.CURRENT_USER!.uid).child("personalLocations/\(parkId!)")

        ref.observe(.value, with: { (snapshot) in
            self.ref.setValue(nil)

            //call to delegate
            self.delegate?.mainRefresh()
        })
    }
}

MapView类(实现DeleteVCDelegate)

class mapVC: MKMapViewDelegate, DeleteVCDelegate{

    //when you present your DeleteVC set its delegate to the map view
    let vc=(self.storyboard?.instantiateViewController(withIdentifier: "deleteVC"))! as! DeleteVC

    //set the delegate
    vc.delegate=self

    //present deleteVC
    self.present(vc, animated: true, completion:nil)

    //implement delegate method of DeleteVC
    func mainRefresh(){
        //dismiss modal
        self.dismiss(animated: true) { 
            //update view
            self.loadLocations()
            self.annotationRefresh()
        }

    }
}