Reputation: 59
Right now I have two tabs: Tab #1 Has a map and a pinned location. Tab #2 Has the address of the pinned location.
What I want to happen is that when I delete the pin on tab #1, the data on tab #2 should be set to nothing. However, what happens is that the information is still present on the 2nd tab even after I delete the pin.
Here is my code for tab #1:
@IBAction func trashButtonSelected(sender: AnyObject) {
// Remove from NSDefaults
// Show alertview
let alertController = UIAlertController(title: "Are you Sure?", message: "Do you wish to delete your pinned Location?", preferredStyle: .ActionSheet);
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (Action) in
// Cancel
}
let deleteButton = UIAlertAction(title: "Delete", style: .Destructive) { (Action) in
NSUserDefaults.standardUserDefaults().removeObjectForKey("pinnedLocation");
NSNotificationCenter.defaultCenter().postNotificationName("reloadData", object: nil);
self.map.removeAnnotations(self.map.annotations);
self.pinLocationButton.enabled = true;
}
alertController.addAction(deleteButton);
alertController.addAction(cancelAction);
self.presentViewController(alertController, animated: true, completion: nil);
So I send a notification called reloadData that should reset all the data on tab #2.
here is my code for tab #2:
in my viewDidLoad
method I have:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reloadData:", name: "reloadData", object: nil);
And then in my selector method:
func reloadData(notification:NSNotification) {
smallMapView.removeAnnotations(smallMapView.annotations);
smallMapView.showsUserLocation = true;
let location = locationManager.location;
let latitude = location!.coordinate.latitude;
let longitude = location!.coordinate.longitude;
let latDelta:CLLocationDegrees = 0.001;
let longDelta:CLLocationDegrees = 0.001;
let span: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta);
let overallLoc = CLLocationCoordinate2DMake(latitude, longitude);
let region:MKCoordinateRegion = MKCoordinateRegionMake(overallLoc, span);
self.smallMapView.setRegion(region, animated: true)
completeAddressPinned = "NO ADDRESS INFO"
}
Any help would be greatly appreciated!
Upvotes: 1
Views: 253
Reputation: 41
In your tab#2: you should add the observer on the init method not on viewDidload method :
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
NSNotificationCenter.defaultCenter().addObserver(self,selector:#selector(self.shouldReloadFavoritPost(_:)),name:"shouldReloadFavorit",object:nil)
}
Upvotes: 2