What happens if I don't retain IBOutlet?

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

If I do this:

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

ins

6条回答
  •  抹茶落季
    2020-11-29 17:02

    Well, in the second case you're adding a getter/setter method for that particular IBOutlet. Any time you add a getter/setter method you (almost always) want to have it set to retain for memory management issues. I think a better way to have posed you're question would have been this:

    @interface RegisterController : UIViewController 
    {
    IBOutlet UITextField *usernameField;
    }
    @property (nonatomic) IBOutlet UITextField *usernameField;
    

    or

    @interface RegisterController : UIViewController 
    {
    IBOutlet UITextField *usernameField;
    }
    @property (nonatomic, retain) IBOutlet UITextField *usernameField;
    

    In that case, then yes, you would need to add a retain since it will affect memory management. Even though it may not have any effects, if you're programatically adding and removing IBOutlet's, you could potentially run into issues.

    As a general rule: always add an @property (with retain) whenever you have an IBOutlet.

提交回复
热议问题