"iOS Development series" XIB iboutlets use strong or weak?

Source: Internet
Author: User

Someone asked, under the arc, Iboutlets should be defined as strong or weak? The answer is supported by the largest number of people, with only one excerpt 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:

  • < P style= "margin-top:0px; Margin-bottom:1em; padding-top:0px; padding-bottom:0px; border:0px; List-style:none; Word-wrap:break-word; Word-break:normal; line-height:21px; Vertical-align:baseline; Clear:both "> 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).

  • to the effect of saying thatin 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, the custom view is not directly displayed as a sub view in main view, but is created by instantiation. You instantiate yourself, of course, you need to strong, otherwise who will retain the ownership of the object for you?


The above analysis is not wrong, but always feel that something is missing. For the end is weak or strong, in the final analysis, or to the object of the ownership of the problem, but it is not easy to summarize the simple and understandable rules of use. So, there will be one exception to break the document summary of the general, do not understand the root of the rule is what, or will encounter trouble.


Let me give you a simple example of creating a program entry that points to the navigation controller's project, and the navigation bar with 2 buttons:


The right button controls whether the camera button is displayed or not, and according to the documentation, we define the 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 buttons:

#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 result is that the first time the camera button is hidden, it is no longer visible. The reason is simple, camerabtn point to empty, we lost camerabtn object ownership.

There are two ways to solve a problem:

1. Do not drag the camera button on the Xib or storyboard, but instead use the code to create and control the ownership of the object

2. Define CAMERABTN as Strong

The second way I want to emphasize is of course


By the way, the rules for other ARC properties:

  • Strong: Equivalent to "retain", property becomes owner of object

  • Weak: property is weak pointer and automatically set to nil when object is disposed

  • 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

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"iOS Development series" XIB iboutlets use strong or weak?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.