【问题标题】:is it right way in obj-c programming在 obj-c 编程中是正确的方式吗
【发布时间】:2012-01-02 12:37:35
【问题描述】:

我想知道我编程的方式是否正确。你能帮帮我吗?

@interface ViewController : UIViewController
{
    UILabel *messageLabel;
}
@end

当我在 .h 中声明新对象并在 .m 中创建时,我必须使用@property (nonatomic, retain) UILabel *messageLabel?我看到很少有列表,其中当对象由代码 @property 创建时不存在,并且很少存在,我很困惑。

当我通过代码创建对象时不使用@property(例如UILabelUIImageViewUIButton)是否正确?

【问题讨论】:

  • 如果你打算使用这些对象中的信息在视图之间传递,你只需要@property。

标签: objective-c coding-style properties implementation declare


【解决方案1】:

@property 是一个关键字,可用于为该字段自动创建 setter/getter 方法。如果您不需要从视图控制器外部访问您的标签,则无需使用 @property 并且您的代码很好。

【讨论】:

    【解决方案2】:

    您的代码是正确的。 @property 在整个应用程序中全局使用,而不是特定于您的 UIViewController。如果您只打算引用或更改标签/其他组件,请在 { } 中定义它,否则应使用 @property

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-02
      • 1970-01-01
      • 2020-07-17
      • 2023-03-23
      • 2011-10-17
      • 2015-02-07
      • 1970-01-01
      相关资源
      最近更新 更多