views:

199

answers:

2

Hi have an newbie issue with objective-c, cocoa & iPhone. I've declared mapView in my application delegates applicationDidFinishLaunching:application: which is instance/pointer of MKMapView class. mapView is a member of my application delegate.

In my view controller in viewDidLoad: I get instance of my application delegate with the following:

appDelegate = [[UIApplication sharedApplication] delegate];

My view controller also has got MKMapView *mapView as a member. In viewDidLoad: I try to set it this way:

mapView = [appDelegate mapView]; 

but it seems that I'm not able to get pointer or "reference" to the actual mapView because when I try to [mapView setRegion:region animated:YES]; it does not work. How ever [[appDelegate mapView] setRegion:region animated:YES]; does work.

So the question is how do I get a pointer to appDelegates mapView from my view controller?

A: 

You're not retaining the appDelegate. If you created your delegate like this:

@property (retain) id appDelegate;

Then you have to actually call set on it to retain it:

[self setAppDelegate:[[UIAppliation sharedApplication] delegate]];

Better yet, use assign in your declaration:

@property (assign) id appDelegate;

Best regards,

Matt Long
I can't imagine this is the problem.
Chuck
Yeah. Sorry. Misread the question. Thanks for the down vote though. Yer a sweetheart. ;-)
Matt Long
I didn't downvote; I just commented.
Chuck
+1  A: 

It sounds like you're caching your app delegate's mapView member. However, it's possible that at the time you perform this cache, it's not yet instantiated (setting a breakpoint at this location will reveal that to you).

The answer to your question is: [appDelegate mapView] returns a pointer to the appView member. However, if that member is nil, that's what you'll get back.

Ben Gottlieb
I moved the assignment of `mapView` to `viewWillAppear:animated:` and it is now working perfectly. In addition I made `appDelegate` and `mapView` properties of the view controller as suggested by Matt Long. (didn't try without them thou)
Eino T
It definitely sounds like you were loading your view before you were creating your mapView.
Ben Gottlieb