我环顾四周,阅读了大量博客以了解如何实现我的目标。我遇到的最容易理解的帖子是这里的帖子 Pass data back to previous viewcontroller .我确定我的理解是混淆的,但我想要完成的是在我的第二个 View 中滑动单元格时从 map 中删除注释。
从 CoreData 中删除注释不是问题,当我单击 rightCallOut 时删除引脚也不是问题。当我想从 VC2 中的操作中删除 VC1 中 map 中的注释时,问题就来了。我在哪里误解了这个简单的过程,我该如何完成它?
第一 View Controller
import UIKit
class ViewController: UIViewController, PinRemoverDelegate {
func removePin() {
mapView.removeAnnotation(selectedAnnotation)
}
}
第二 View Controller
import UIKit
protocol PinRemoverDelegate: class {
func removePin()
}
class SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
weak var delegate: PinRemoverDelegate? = nil
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let place = storedLocations[indexPath.row]
context.delete(place)
(UIApplication.shared.delegate as! AppDelegate).saveContext()
// Attempt To remove The Pin
delegate?.removePin()
}
tableView.reloadData()
}
}
最佳答案
误解只是如何取下引脚。调用 removePin 函数是错误的。只需重新加载 CoreData,因为引脚已从 CoreData 中移除。
关于ios - Swift 3 协议(protocol)和委托(delegate)方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42989123/