Question

When declaring a button, there are always two options as properties for the button:strong and weak. What is the difference between them? Also, what it nonatomic? For example:

@property (weak, nonatomic) IBOutlet UIButton *MyButton;

No correct solution

OTHER TIPS

Got this all from http://clickflickboom.com/strong-vs-weak/...

You have a choice between (strong) and (weak) when defining a property. In this post I’ll explain the difference between the two.

Strong

@property (strong) NSString *myString;

This is the default state of a pointer, though I still like to explicitly state strong for clarity. A strong pointer will be retained as long as the class in which it was allocated remains.

Weak

@property (weak) NSString *myString;

A weak reference means the pointer has no owner, therefore it will be deallocated as soon as it is no longer needed (that is, nothing else is pointing to it).

Most commonly, you would use weak for IBOutlets, such as UITextFIeld, UILabels and UIButton objects. Here’s an example:

In this case, I’m creating an IBOutlet for a button (theButton) in my view within my controller. The theButton object belongs to my view, not my controller, so in this case a weak attribute makes more sense than strong. In fact, as a general rule IBOutlets should always be set to weak.

please read this:

Should IBOutlets be strong or weak under ARC?

From a practical perspective, in iOS and OS X outlets should be defined as declared properties. Outlets should generally be weak, except for those from File’s Owner to top-level objects in a nib file (or, in iOS, a storyboard scene) which should be strong. Outlets that you create will therefore typically be weak by default, because:

  1. Outlets that you create to, for example, subviews of a view controller’s view or a window controller’s window, are arbitrary references between objects that do not imply ownership.

  2. The strong outlets are frequently specified by framework classes (for example, UIViewController’s view outlet, or NSWindowController’s window outlet).

    @property (weak) IBOutlet MyView *viewContainerSubview;
    @property (strong) IBOutlet MyOtherClass *topLevelObject;
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top