gpt4 book ai didi

swift - 使用不同的 View Controller 删除 map 上的数据

转载 作者:行者123 更新时间:2023-11-30 12:24:48 25 4
gpt4 key购买 nike

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

我面临的问题是,当用户按下删除键(从 Firebase 中删除该位置)时,注释仍然显示在 map 上。我无法在确实出现的 View 中重新加载注释数据,因为这不适合我的应用程序。我不能每次打开 map View 时都重新加载注释。

我需要找到一种方法来实现按下删除按钮时重新加载注释。但是,由于这种情况发生在我的删除 View Controller (不包含 map View )中,我无法使用重新加载功能。有没有办法连接 View Controller ,以便我可以在按下删除时应用重新加载功能?

更新代码**

这是我的 map View Controller :

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)

}



}

这是我的删除 View Controller :

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


})



}

}

最佳答案

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

模态删除 View

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

}
}

关于swift - 使用不同的 View Controller 删除 map 上的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44336366/

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