What happens if I don't retain IBOutlet?

后端 未结 6 1489
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 16:13

If I do this:

@interface RegisterController : UIViewController 
{
    IBOutlet UITextField *usernameField;
}

ins

6条回答
  •  無奈伤痛
    2020-11-29 16:59

    There isn't any difference between the way those two interface definitions work until you start using the accessors provided by the property.

    In both cases, you'll still need to release and set-to-nil the IBOutlet in your dealloc or viewDidUnload methods.

    The IBOutlet points to an object instantiated within a XIB file. That object is owned by the File's Owner object of the XIB file (usually the view controller that the IBOutlet is declared in.

    Because the object is created as a result of loading the XIB, it's retain count is 1 and is owned by your File's Owner, as mentioned above. This means that the File's Owner is responsible for releasing it when it's deallocated.

    Adding the property declaration with the retain attribute simply specifies that the setter method should retain the object passed in to be set - which is the correct way to do it. If you did not specify retain in the property declaration, the IBOutlet could possibly point to an object that may not exist any more, due to it being released by its owner, or autoreleased at some point in the program's lifecycle. Retaining it prevents that object being deallocated until you're done with it.

提交回复
热议问题