ios - Swift 3 协议(protocol)和委托(delegate)方法?

标签 ios swift swift3 delegates

我环顾四周,阅读了大量博客以了解如何实现我的目标。我遇到的最容易理解的帖子是这里的帖子 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/

相关文章:

ios - 在 RubyMotion 应用程序中实现类似 "current_user"的方法

SwiftyJSON YouTube API

swift - swift 中 Any.Type 的大小

ios - 在 Swift 3 UI 测试中访问自定义 View 组件中的元素

ios - 等待超时时发送信号量信号

ios - Swift SearchResultsController Segue - "Receiver ... has no segue with identifier ' testSeg ''"

ios - 备份 Realm 到 iCloud Drive

ios - UISearchBar 结束编辑,隐藏键盘,但屏幕仍然变暗

ios - 如何每30秒切换一次基于NSTimer的执行?

ios - 创建单独的文件