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;

没有正确的解决方案

其他提示

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;
    
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top