What is the difference between IBOutlet as a prope

2020-02-14 06:15发布

There are two different methods to declare IBOutlet.

  1. In @interface section as variable:

    IBOutlet UIButton *exampleButton;
    
  2. Below the curve bracket but before @end of .h file as property:

    @property (nonatomic, retain) IBOutlet UIButton *exampleButton;
    

What is the difference between these two methods and where should I use each one? Which method is better and in what cases?

2条回答
We Are One
2楼-- · 2020-02-14 06:55

Either one works fine in my experience. What doesn't work is declaring both the instance variable and the property "IBOutlet" -- that seems to really confuse things. If for some reason you want to avoid providing public access to your outlet, you can declare it as an instance variable and simply not create the property. On the other hand, now that the runtime will synthesize instance variables for you, many people are declaring only properties and skipping the explicit instance variable declaration; in that case, you'd obviously declare the property as the IBOutlet.

查看更多
Luminary・发光体
3楼-- · 2020-02-14 07:15

The @property combined with @synthesize setup the getter and setter methods for your objects. You should define it at least in the interface, and if you decide to create a property from it then you must also synthesize it the .m file.

查看更多
登录 后发表回答