我创建了一个通知,该通知将在位置更新可用时发送:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// If we don't have a valid location, exit
guard let location = locations.first where (location.horizontalAccuracy <= 20 || location.timestamp.timeIntervalSinceNow < 5 ) else {
return
}
// or if we already have searched, return
guard !didPerformGeocode else {
return
}
// otherwise, update state variable, stop location services and start geocode
self.didPerformGeocode = true
// Set current user location
currentUserLocation = location
// Send notification about changed location
notificationCenter.postNotificationName("LOCATION_AVAILABLE", object: nil, userInfo: ["location":self.currentUserLocation!])
// Stop location search before geocode
self.locationManager?.stopUpdatingLocation()
self.geocodeLocation(location)
}
我想在tableViewController中收到此通知:
class AEDTableViewController: UITableViewController {
// MARK: - Notifications
let notificationCenter = NSNotificationCenter.defaultCenter()
// MARK: - Location
var currentLocation: CLLocation = CLLocation()
// MARK: - View Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
notificationCenter.addObserver(self, selector: "aedListReceived:", name: "AED_LIST_UPDATED", object: nil)
notificationCenter.addObserver(self, selector: "locationAvailable:", name: "LOCATION_AVAILABLE", object: nil)
}
deinit {
notificationCenter.removeObserver(self)
}
func locationAvailable(notification: NSNotification) {
let userInfo = notification.userInfo as! Dictionary<String, AnyObject>
self.currentLocation = userInfo["location"] as! CLLocation
self.tableView.reloadData()
}
}
但即使我看到更新的位置,也不会收到通知。当我想调试它时,我发现它从不调用notificationObserver中提到的方法。