user2121361
user2121361

Reputation:

Swift 3 Protocol and Delegate Method?

I've Looked around SO, and read a good number of blogs to find out how to accomplish my goal. The most understandable post I came across was the one here Pass data back to previous viewcontroller. I'm sure my understanding is mixed up but what I am trying to accomplish is removing an annotation from the map when I swipe a cell in my second view.

Removing the annotation from the CoreData is not a problem, removing the pin when I click the rightCallOut is not an issue either. The problem comes when I want to remove the annotation from the map in VC1 from an action in VC2. Where am I misunderstanding this simple process and how do I accomplish it?

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

}

Upvotes: 3

Views: 3542

Answers (1)

user2121361
user2121361

Reputation:

The misunderstanding is only how to remove the pin. Calling a removePin function is wrong. Simply reload the CoreData, since the pin has already been removed from the CoreData.

Upvotes: 1

Related Questions