简体   繁体   English

将视图声明为属性v。临时创建

[英]Declaring a View as a property v. Temporary Creation

I have recently began creating iOS applications completely programmatically (without the interface builder) and was wondering if there was any advantage/difference in declaring a ViewController's view as a property before using it versus simply creating at the loadview function. 我最近开始完全以编程方式(没有界面生成器)创建iOS应用程序,并且想知道在使用ViewController的视图之前将其声明为属性,而不是简单地在loadview函数中进行创建是否有任何优势/差异。 Also, would I dealloc the view inside of the controllers dealloc if I am using it as a property? 另外,如果我将视图用作属性,是否可以在控制器的dealloc中取消分配视图?

ie this 即这个

- (void)loadView
{
    _rootView = [[RootView alloc] initWithFrame:CGRectZero];
    [self setView:self.rootView];
}

vs.

- (void)loadView
{
    RootView *rootView = [[RootView alloc] initWithFrame:CGRectZero];
    [self setView:rootView];
    [rootView release];
}

view already is a property of UIViewController . view已经 UIViewController的属性。 Declaring an extra property such as rootView in your example would be pointless. 在您的示例中声明一个额外的属性(例如rootView将毫无意义。 So your second example would be the way to go. 因此,您的第二个例子就是要走的路。 (I'm not sure why you'd want to create a view with a width and height of zero, but that's another story.) (我不确定为什么要创建宽度和高度为零的视图,但这是另一回事了。)

In this case, your dealloc implementation (if you provide one) should call [super dealloc] to ensure that the view property is sent a release message, but of course you should always call [super dealloc] in any overridden implementation of dealloc . 在这种情况下,您dealloc实现(如果你提供一个)应该调用[super dealloc]以确保view属性发送一个release消息,当然你应该始终调用[super dealloc]在任何重写实现dealloc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM