Piero
Piero

Reputation: 9273

create every time a new uiviewcontroller or reuse the same?

I want to know which is the better way: Either to create a new uiviewcontroller for instance, if in a view i have to load a search view to search new element on a web server, or load a view that display the result from a request, Which is the better way?

This:

MasterViewController.h

@property (nonatomic, strong) PreviewViewController *reviewViewController;

MasterViewController.m

-(void)openPreviewView
{
if (!self.previewViewController) {
    self.previewViewController = [[PreviewViewController alloc] init];
}

[self.navigationController pushViewController:self.previewViewController animated:YES];
}

or this:

MasterViewController.m

-(void)openPreviewView
{
PreviewViewController *previewView = [[PreviewViewController alloc] init];

[self.navigationController pushViewController:previewView animated:YES];
}

Upvotes: 1

Views: 1309

Answers (2)

rmaddy
rmaddy

Reputation: 318934

I always create each view controller as it is needed. The only time I keep a view controller around is if, after doing some proper performance testing, I find that keeping a specific view controller around is better for the user and for performance. Of course you must deal with memory warnings to properly cleanup any cached view controllers.

Don't worry about performance optimizations too early. Wait until you find, through real testing, that you actually have an issue to worry about.

Upvotes: 4

davidf2281
davidf2281

Reputation: 1328

The first one. iOS devices are resource-constrained, so in the absence of a compelling reason not to, you should always re-use if you can.

Upvotes: 1

Related Questions