I'm trying to use the annotations array from my map view in another class but the copy I've made in the new class is null and I'm not sure why. In the class with the map view it has 103 annotations. Any ideas?
Code:
RSFM *rsfm = [[RSFM alloc]init];
NSArray *annotations = [rsfm.worldView.annotations mutableCopy];
NSLog(@"Annotations Array: %@ Count: %d", annotations, [annotations count]);
Several things.
I'm assuming that your RSFM class is a view controller class?
If it is, then I'm further assuming that the RSFM class's worldView property is a map view object (MKMapView)?
You are breaking one of the big rules of MVC. Don't manipulate another view controller's view objects directly. Treat them as the private property of the other view controller.
In this case, you are failing because a view controller doesn't create it's view hierarchy until the first time it's displayed on the screen (or you try to reference self.view in your code.) Before that, all a view controller's outlets are nil.
You should add a loadViews method to your RSFM class that causes it to load it's view hierarchy. It can be as simple as this:
- (void) loadViews;
{
UIView *myView = self.view; //Force the views to load
}
Next, you need to add code to your view controller that causes it to install whatever annotations it should load into it's map view. How you do that is app-specific.
Finally, you should add a myMapAnnotations property to your RSFM class. When you build your array of annotations and install it in your map view, also save it in your myMapAnnotations property.
Then, in your other object, read the myMapAnnotations property instead of trying to fetch the annotations from the RSFM object's map view.