Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reuse UIViewController instances when using storyboard

I decided to give the use of storyboards a go in my current iPhone app. I am facing a bit of a problem. I really need to reuse my UIViewController instances.

What do I mean by that? Well, for example I have a table view controller. When I tap a cell, another view controller is loaded from the storyboard and pushed onto the navigation controller stack. This all works well, but it takes about half a second to a second each time this view controller is loaded. Before I was using story boards I simply solved this problem by caching the created instance so the second time you tap a cell the view controller can be immediately shown.

By caching the created instance I mean something like this:

if (!cachedInstance) {
    cachedInstance = [MyViewController new];
}
[self.navigationController pushViewController:cachedInstance];

Does anyone know how to accomplish this using the storyboard? Thanks in advance.

like image 212
Tom van Zummeren Avatar asked Jul 24 '12 14:07

Tom van Zummeren


People also ask

What is the name of the UI view controller that can use to acquire high reusability with less number of activities?

swift - reusable view controller with view In storyboard (multiple in one scene) - Stack Overflow.

What is the difference between ViewController and UIViewController?

Both are used for different purpose. A UIViewController class manages a ViewContoller which is responsible for actions that happen within that View controller. This class is aware of actions that happen on view controller, like ViewDidLoad, ViewWillApper, ViewDidAppear, ViewWillDisapper, ViewDidDisapper.

Is UIViewController a subclass of Uiview?

Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy. A view controller's main responsibilities include the following: Updating the contents of the views, usually in response to changes to the underlying data.


Video Answer


1 Answers

If you are using segues, you will need to create custom segues in order to use a cached view controller like you did before. Otherwise, the typical "push" segue will create a new instance of the view controller for segue.destinationViewController. If you write a custom UIStoryboardSegue class and use custom segues you can override initWithIdentifier:source:destination: and put your cached view controller in for the destinationViewController, and then override perform to use the classic pushViewController call.

That is how you handle the segue if you are really intent on using them. I would just skip it though, unless you really want the fancy arrows to lay everything out on your storyboard. If you skip it you can just instantiate the view controllers into the cache and then push them on just like you did before.

If your question is more about locating a view controller inside a storyboard then you can use:

UIViewController *vc = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"Some View Controller"];

Then you can save that to your cache and push it like you did in your example code.

Hope that helps.

like image 164
Justin Paulson Avatar answered Oct 15 '22 16:10

Justin Paulson