Someone asked. Under Arc, should iboutlets be defined as strong or weak? The person who supports this answer is the most. The answer is just a snippet from an official document:
From A Practical Perspective, in IOS and OS X outlets should is defined as declared properties. Outlets should generally is weak, except for those from File's Owner to top-level objects in a nib File (or, in IOS, a sto Ryboard scene) which should be strong. Outlets that you create would therefore typically be weak by default, because:
-
outlets that's create to, for example, Subviews of a View controller ' s view or a window controller ' s window, is arbitrary references between objects that does not imply ownership.
-
The strong outlets is frequently specified by framework classes (for example, Uiviewcontroller ' s view outlet, or Nswindow Controller ' s window outlet).
-
the effect is that. in theARC , the general outlet property is recommended to useWeak,should use strong of theoutlet is aFile ' s owner connects to nib the top-level object.
What is the File ' s owner attached to the top-level object of the nib? White words. Is the view that you define, not directly displayed as a sub view in Main view. But it needs to be created by instantiation.
You instantiate it yourself. Of course, the need to strong, or who will keep all the rights of the object for you?
The above analysis is not wrong, but always think that something is missing.
For whether it is weak or strong, in the final analysis, or to the object of the whole right to the problem, but it is not easy to summarize the simple and easy to understand the regularity of the use of the method. So. There will be one exception. Breaking the rules summarized in the document, it is not clear what the root of the rule is, or it will run into trouble.
Let me give you a simple example, create a program entry to project with the navigation controller, and drag 2 buttons on the navigation bar:
The right button controls whether the camera button is displayed or not. Follow the instructions in the documentation. We define these two buttons as weak properties in the program
#import @interface testviewcontroller:uiviewcontroller { BOOL isshowing; } @property (nonatomic,weak) iboutlet Uibarbuttonitem *controlbtn; @property (nonatomic,weak) iboutlet Uibarbuttonitem *camerabtn; -(Ibaction) Controlaction: (ID) sender;
Use the right button to control the hiding and display of the camera button:
#import "TestViewController.h" @interface testviewcontroller () @end @implementation Testviewcontroller @synthesize camerabtn,controlbtn; -(void) viewdidload { [super viewdidload]; Additional setup after loading the view, typically from a nib. isshowing = YES; } -(Ibaction) Controlaction: (ID) sender { if (isshowing) { self.controlBtn.title = @ "show camera"; Self.navigationItem.leftBarButtonItem = nil; isshowing = NO; } else { Self.controlBtn.title = @ "Hide Camera"; Self.navigationItem.leftBarButtonItem = camerabtn; isshowing = YES; } }
The experimental results are. When the camera button is hidden for the first time, it is no longer visible. The reason is very easy,camerabtn pointing to empty. We lost all rights to the Camerabtn object.
There are two ways to solve this problem:
1. Do not drag the camera button on the Xib or storyboard, but use the code to create, you control the entire rights of the object
2. Define CAMERABTN as Strong
What I want to emphasize is, of course, another way.
By the way, the rules for other ARC properties:
-
Strong: Equivalent to "retain", property becomes owner of object
-
Weak: property is weak pointer, which is set to nil when the object is released
-
Unsafe_unretained: Equivalent to the previous "assign", only IOS 4 should be used
-
Copy: Copy an object and create a strong association as in the previous copy
-
Assign: The object cannot use assign, but the original type (BOOL, int, float) can still be used
"iOS Development series" XIB iboutlets use strong or weak?