I have a tab based application. One of the tabs contains an MKMapview.
When I first launch the app, my memory is around 44 mb.
When I switch to the tab with the map, it jumps right up to 84 mb.
Furthermore, I have added two buttons for zooming in and out as requested by the client.
After just two clicks of the zoomIn button, it jumps to 104 and then 2 clicks of the zoomOut it jumps to 112.
I want to nil out my map view in viewDidDissappear like so to free up some memory:
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
self.mapView = nil
}
Then I would need to change my setUpMaps func to be called on viewDidAppear instead of viewDidLoad. It is as follows:
func setUpMaps(){
locManager = CLLocationManager()
locManager.delegate = self
locManager.startUpdatingLocation()
self.mapView.delegate = self
self.mapView.pitchEnabled = true
self.mapView.showsBuildings = true
let latitude:CLLocationDegrees = Prefs.latitude
let longitude: CLLocationDegrees = Prefs.longitude
let theRegion:MKCoordinateRegion = MKCoordinateRegionMake(CLLocationCoordinate2DMake(latitude,longitude), MKCoordinateSpanMake(0.9, 0.9))
self.mapView.setRegion(theRegion, animated: true)
let thePoint = MKPointAnnotation()
thePoint.coordinate = CLLocationCoordinate2DMake(Prefs.latitude as CLLocationDegrees, Prefs.longitude as CLLocationDegrees)
thePoint.title = "Your registered location"
self.mapView.addAnnotation(thePoint)
addRadiusCircle(CLLocation(latitude: Prefs.latitude as CLLocationDegrees, longitude: Prefs.longitude as CLLocationDegrees), radiusParam: CLLocationDistance(Double(Prefs.radius)))
}
Obviously this crashes the second time as mapView is nil. So I need to give it a value again. Something like :
var mapView = MKMapView()
But I have mapView defined as an outlet:
@IBOutlet weak var mapView: MKMapView!
Is there any way to reassociate the var mapView with my outlet in the viewDidAppear func? Or should I just get rid of the outlet altogether and create the mapView programmatically? If defining programatically should I be usinig weak var?
zoom funcs:
@IBAction func zoomIn(sender: UIButton) {
let span = MKCoordinateSpan(latitudeDelta: mapView.region.span.latitudeDelta/2, longitudeDelta: mapView.region.span.longitudeDelta/2)
mapView.setRegion(MKCoordinateRegion(center: mapView.region.center, span: span), animated: true)
}
@IBAction func zoomOut(sender: UIButton) {
let span = MKCoordinateSpan(latitudeDelta: mapView.region.span.latitudeDelta*2, longitudeDelta: mapView.region.span.longitudeDelta*2)
mapView.setRegion(MKCoordinateRegion(center: mapView.region.center, span: span), animated: true)
}