简体   繁体   中英

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. Also, would I dealloc the view inside of the controllers dealloc if I am using it as a property?

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 . Declaring an extra property such as rootView in your example would be pointless. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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