PDA

View Full Version : IBOutlet and static variables?




MACloop
Jul 8, 2010, 03:17 AM
Hello,
I have a map defined as a IBOutlet and it is showed in a viewcontroller. This map is only created once and I really do not need a local variable for it. The viewcontroller lives as long as the app is running, and the same is about the map. I have defined maps like this as static before but have not been using IB in those cases. So, is it possible to define a varaiable as static even if it is defined in IB? Or may I do something like this instead?

@IBOutlet MKMapView *map;

and the use map as normal in the viewconteroller?

//for instance like
map.userLocation.title = @"You are here!";
map.showsUserLocation = YES;
region.span=span;
region.center=someLocation;
[map setRegion:region animated:NO];
[map regionThatFits:region];

//and like...
[map addAnnotation:theAnnotation];

What is when a memory warning is received? Is the map recreated or is the already available map overwritten? Because I have not defined a retain in my code I suppse it may not lead to a leak?

MACloop



adrian.oconnor
Jul 8, 2010, 04:29 AM
I've never seen IBOutlets defined as static before. I haven't fired up XCode to check (and I wouldn't be at all surprised to find out that I'm wrong), but I would not expect IB to let you map an interface element to a static variable (nor an action to a static method).

Why do you actually want to use a static variable? The single-instance argument seems moot to me, because you only have one single instance of the view controller for the duration of the application anyway. Creating a local variable will achieve the exact same result as using a static variable -- one single instance. If it's because you want to access the variable from anywhere in your code (without having a reference to the instance of the class), you could always keep a reference to the view controller in your app delegate and use the [[UIApplication sharedApplication] delegate] helper to get at the reference.

Did you try mapping the element to the static IBOutlet in IB? I assume you did, and that you couldn't see the property (and that's why you posted here).

MACloop
Jul 8, 2010, 05:04 AM
I've never seen IBOutlets defined as static before. I haven't fired up XCode to check (and I wouldn't be at all surprised to find out that I'm wrong), but I would not expect IB to let you map an interface element to a static variable (nor an action to a static method).

Why do you actually want to use a static variable? The single-instance argument seems moot to me, because you only have one single instance of the view controller for the duration of the application anyway. Creating a local variable will achieve the exact same result as using a static variable -- one single instance. If it's because you want to access the variable from anywhere in your code (without having a reference to the instance of the class), you could always keep a reference to the view controller in your app delegate and use the [[UIApplication sharedApplication] delegate] helper to get at the reference.

Did you try mapping the element to the static IBOutlet in IB? I assume you did, and that you couldn't see the property (and that's why you posted here).

Thanks alot for the answer! I think, exactly as you write that it is unneccesary to define the map as static in this case. I only wanted to ask if it is safe to do this way. Your comment gave me all the answers I need and I will go for the approach I described in the post earlier. Again - thanks alot for the comment!
MACloop

PhoneyDeveloper
Jul 8, 2010, 08:00 AM
You should always declare an @property for your IBOutlets. Usually they're retain properties but can also be assign.