views:

774

answers:

4

The code generator Accessorizer has an option to assign IBOutlets rather than retaining them. For example, compare these two generated lines:

@property(nonatomic,retain)IBOutlet UIImageView *backgroundView;
@property(nonatomic,assign)IBOutlet UIImageView *backgroundView;

Why would I want to assign IBOutlets, while retaining all other properties?

A: 

Take a look at this question retain vs assign

ennuikiller
Thanks, but I'd like to know why someone would want to assign IBOutlets, but not other types of objects
Casebash
+3  A: 

Usage depends on the platform as specified in the memory management guide :

"The behavior of outlets depends on the platform (see “Mac OS X Desktop” and “iPhone”), so the actual declaration differs:

  For Mac OS X, you should use:

  @property (assign) IBOutlet UserInterfaceElementClass *anOutlet;

  For iPhone OS, you should use:

  @property (nonatomic, retain) IBOutlet UIUserInterfaceElementClass *anOutlet;"
Tyr
There's nothing wrong with using (retain) on Mac OS X, you just need to balance your retains and releases.
Jon Hess
+1  A: 

On the iPhone OS, your view may get unloaded (under low memory conditions), but if you have state that you care about in some of your views connected to outlets, you don't want to lose it. So you want to retain them.

Say a navigation controller. The root view gets unloaded from a low memory warning sent to the controller but there is a bunch of controller above it on the stack. When you pop back to root, it reloads the view and sticks your outlets back where they were in the same state they used to be in.

At least, I think this is the technical reason.

But in a more general sense, your controller cares about these views, because it wants to interact with them. And that fact alone means that you want to retain them, and release them when you no longer care. It's just good practice.

Squeegy
A: 

IBOutlet is defined as nothing. Command-click on the word in Xcode to see its definition.

Which is to say, it's just a hook so interface builder knows to show it. I've never seen an @property which included IBOutlet, but I imagine it's for situations like UIViewController's @property view which needs to be accessible from IB, but can also be assigned at runtime.

Kenny Winker