What is the difference between IBOutlet as a property or as a variable?

柔情痞子 提交于 2019-11-28 08:49:44

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/6681117/what-is-the-difference-between-iboutlet-as-a-property-or-as-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!