IBOutlet作为属性或变量有什么区别? [英] What is the difference between IBOutlet as a property or as a variable?

查看:93
本文介绍了IBOutlet作为属性或变量有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两种不同的方法来声明IBOutlet.

There are two different methods to declare IBOutlet.

  1. 在@interface部分中作为变量:

  1. In @interface section as variable:

IBOutlet UIButton *exampleButton;

  • 位于.h文件的曲线括号下方但在@end之前作为属性:

  • 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?

    推荐答案

    根据我的经验,任何一种都可以正常工作.无效的是声明实例变量和属性"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.

    这篇关于IBOutlet作为属性或变量有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆