2

For my app, I want to have a few different instances of the same view controller. For now, I am just creating a new instance like this:

iSafeViewController *tab = [[iSafeViewController alloc] init];

[tab setModalPresentationStyle:UIModalPresentationFullScreen];
[tab setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

[self presentViewController:tab animated:YES completion:nil];

Great. And since this is done in the iSafeViewController class anyway, I have another button that currently just dismisses the latest controller on the stack.

 - (IBAction)closeTab:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
 }

Okay, however, I really want to be able to go back to these instances. So, I have two questions.

  1. Does dismissViewControllerAnimated remove that controller's instance from memory. If not, is there a way I can re-present it.

  2. There is probably a better way to navigate through viewController instances then presentViewControllerAnimated. At the very least, is there a better way to create new instances of one's viewController and be able to navigate to each of them, hopefully not in a stack. In other words, if there are three viewController instances, is there a way I can go from the third to the main one?

Any ideas would be appreciated. Thanks.

Josiah
  • 4,663
  • 2
  • 31
  • 49

1 Answers1

5

"Does dismissViewControllerAnimated remove that controller's instance from memory? If not, is there a way I can re-present it."

Calling dismissViewControllerAnimated does not explicitly remove a view controller from memory, but if no other part of the code is storing a strong reference to the view controller, once the presenting view controller dismisses your VC, it may be deallocated as per the normal memory management system.

So if you ensure something in your code has a reference to your view controller (aside from the VC which is presenting it modally), it won't disappear after being dismissed, and yes this means you can re-use it.

As for "random access" to view controllers: you could use UINavigationController and use methods like popToViewController:animated: and multiple calls to pushViewController:animated: (without animation!) to create the effect of travelling to arbitrary view controllers. This feels like a bit of a hack.

Alternatively, and preferably, you could write your own custom container view controller. This is a view controller that deals with presenting other view controllers. See Apple docs.

Here's a good WWDC video on the subject: Implementing UIViewController Containment

Further reading:

Community
  • 1
  • 1
occulus
  • 16,959
  • 6
  • 53
  • 76
  • Great, but how would I come back to it. I ended up creating a class that stores all the instances of these controllers. However, if I try using ` presentViewController:selectedItem` it throws an exception saying I am trying to present an already active viewController. How can I go back to a controller, defined as `selectedItem` Is there an alternative to `presentViewControllerAnimated` or something? – Josiah Feb 04 '13 at 16:48
  • I'd stop using modally presented view controllers and think about writing a simple container view controller -- see the article I linked to above. – occulus Feb 04 '13 at 16:50