Swift 3协议和委托方法?

时间:2017-03-24 00:02:05

标签: ios swift swift3 delegates

我一直在寻找,并阅读了大量的博客,以了解如何实现我的目标。我遇到的最容易理解的帖子就是Pass data back to previous viewcontroller。我确信我的理解是混乱的,但我想要完成的是当我在第二个视图中滑动一个单元格时从地图中删除注释。

从CoreData中删除注释不是问题,当我单击rightCallOut时,删除引脚也不是问题。当我想从VC2中的动作中删除VC1中的地图中的注释时,问题出现了。我在哪里误解了这个简单的过程,我该如何完成它?

FirstViewController

import UIKit

class ViewController: UIViewController, PinRemoverDelegate {

    func removePin() {
        mapView.removeAnnotation(selectedAnnotation)
    }

}

SecondViewController

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

}

1 个答案:

答案 0 :(得分:1)

误解只是如何移除引脚。调用removePin函数是错误的。只需重新加载CoreData,因为该引脚已从CoreData中删除。

相关问题