1

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];
}

2 Answers 2

4

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.

1

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.

2
  • If you are worried about resources, then the first option is not a good choice. You end up keeping the view controller in memory when it is not being used.
    – rmaddy
    Apr 29, 2013 at 22:38
  • 1
    One caveat though is that you shouldn't create controller properties for every view controller just to save you from having to re-instantiate it. If and only if you are already storing that view controller into a property, then sure, go ahead and re-use it. Otherwise, don't bother with that and just instantiate it every time.
    – iwasrobbed
    Apr 29, 2013 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.