The correct way to unload / reload View controller

I am new to iOS and wondering how to properly implement unloading / reloading the controller.

My application currently has a NavigationController with a MainMenuViewController (custom view controller) configured as a root view controller. Over the life of the application, new ViewControllers are popped / unloaded in the navigation controller. This works fine, the corresponding ViewControllers are initiated (from the NIB) the first time they are pushed onto the stack.

However, now I want to unload one specific ViewController when it popped up, and then reload it automatically when it is pressed again.

I added [self release] to this ViewControllers viewDidDisappear: and it is unloaded, but when I try to click this view again, I get a message sent to the error and reset the dealloc'ed error. So my questions are:

  • Is this the right way to unload a popup ViewController?
  • How to check if a given ViewController is loaded or not?
  • How to force a reboot? Using loadWithNib: then click on the navigation stack?

Hello,

Peter

+4
source share
1 answer

Welcome to iOS programming. Your accident is a memory management problem. This may take a little time, but memory management becomes easier if you just follow one rule:

the object should release everything that it saves (selection is equivalent to saving)

In this case, your view controller frees itself, and it definitely does not save itself. Here's how the sequence works with the navigation controller:

  • The navigation controller is initialized by the root view controller (first in its stack). Lets call it firstViewController

  • The custom action tells firstViewController initialize secondViewController and click it on the navigation controller. In most cases, firstViewController throws an instance of secondViewController after it is clicked. At this point, firstVC is executed using secondVC . Navigation controller now saves secondVC

  • The user touches the back button on the secondVC navigation secondVC . The navigation controller will pull secondVC out of the stack and release it. Until some other object stores it, secondVC will get dealloc'ed.

  • Now the user returns to firstVC . They can perform the same user action that will initiate and push a new secondVC instance.

Hope this helps.

I also recommend that you (for) read the Apple docs and see an example of the code referenced in the infrastructure docs.

http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html

+3
source

Source: https://habr.com/ru/post/1347909/


All Articles