IBOutlet 作为属性或变量有什么区别?
有两种不同的方法来声明 IBOutlet。
在@interface部分作为变量:
IBOutlet UIButton *exampleButton;
在曲线括号下方但在 .h 文件的 @end 之前作为属性:
@property(非原子,保留)IBOutlet UIButton *exampleButton;
这两种方法之间有什么区别,我应该在哪里使用每种方法?哪种方法更好以及在什么情况下更好?
There are two different methods to declare IBOutlet.
In @interface section as variable:
IBOutlet UIButton *exampleButton;
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据我的经验,任何一种都效果很好。不起作用的是声明实例变量和属性“IBOutlet”——这似乎真的很混乱。如果由于某种原因您想避免提供对您的插座的公共访问,您可以将其声明为实例变量,并且只需不创建该属性即可。另一方面,现在运行时将为您合成实例变量,许多人只声明属性并跳过显式实例变量声明;在这种情况下,您显然会将该属性声明为 IBOutlet。
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.
@property 与 @synthesize 结合为您的对象设置 getter 和 setter 方法。您应该至少在接口中定义它,如果您决定从中创建属性,那么您还必须将其合成到 .m 文件中。
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.