Where to initialize something else in the UIViewController

I have a subclass of UIViewController, and I'm trying to figure out what to override so that I can run some initialization code only once for an object instance.

The viewDidLoad method may seem like the obvious answer, but the problem is that viewDidLoad can work more than once if the controller resets the view due to a memory warning. The initWithNibName:bundle: init and initWithCoder: methods also seem like a good choice, but which one should be overridden? The awakeFromNib method is another consideration, but this does not seem to be executing in my view controller.

Is there a way to do this that I am missing?

+6
source share
3 answers

Perhaps you can still use viewDidLoad, but inside use a static boolean to see if you were already there.

 static BOOL didInitialize = NO; if (didInitialize == YES) return; didInitialize = YES; /* initialize my stuff */ 
+4
source

The UIViewControllers designated initializer, the method that all other initializers must call, is -initWithNibName:bundle: If you want to initialize something when your view controller is created, override this method.

-viewDidLoad designed for any installation, which depends on the type of controller. As you noticed, this method can be executed several times, since views can be loaded more than once. -awakeFromNib will not help if your view controller does not exist in it, and even then it makes sense only if the thing you initialize depends on other objects in the same nib.

+5
source

What about +(void)initialize ? What is the initializer of the class that iOS calls for you, once, for the class, as I understand it.

+2
source

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


All Articles