3

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

}
Community
  • 1
  • 1
  • `SecondViewController` should have a `weak var delegate: PinRemoverDelegate?` property that is set to the view controller instance you want to send the message to. Usually you'd assign the `delegate` in some code that owns or is aware of both view controllers. – par Mar 24 '17 at 00:11
  • Ill edit the post, I have implemented a weak var already, I just lost it in the quick copy/shorten code paste. –  Mar 24 '17 at 00:13
  • remember to set the delegate as self in your `FirstViewController`. Something like `vc2.delegate = self`. it depends on how you present the `SecondViewController` – xmhafiz Mar 24 '17 at 01:03

1 Answers1

1

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.