为UIView分配本地@property

时间:2014-03-24 08:26:42

标签: ios objective-c uiview

我有UIView .h和.m文件,在我的主视图控制器中我有这个

PopUpRectangle *RectangeView = [[PopUpRectangle alloc] initWithFrame:CGRectMake(0, 538, 320, 30)]

这允许我显示矩形,但不是在我的视图控制器的每个函数中执行此操作,我想将此UIView指定为本地@property - 因此它可用于每个函数。

请问这是怎么做的?

1 个答案:

答案 0 :(得分:0)

<强> MyClass.h

@interface MyClass : UIViewController

@property (nonatomic, strong) PopUpRectangle *rectangeView; // Public version

@end

<强> MyClass.m

@interface MyClass()

// If you don't want it public remove if from the .h and add here instead

@end

@implementation MyClass

- (void)viewDidLoad
{
    // Alloc initWithFrame here
    // _rectangeView is the getter for rectangeView
    _rectangeView = [[PopUpRectangle alloc] initWithFrame:CGRectMake(0, 538, 320, 30)];
}    

- (void)someRandomMethodIMadeUp
{
    // We're calling the getter for rectangeView again but in a different way, we're calling the ivar.
    [[self rectangeView] setHidden:NO];
}

@end

由于你已经知道@property,为什么你甚至会问这个问题。在询问您似乎已经知道的另一个简单基本问题之前,请先阅读Apple Documentation