Which method is called before viewDidLoad (), but after main?

I noticed that viewDidLoad() is called before didFinishLaunchingWithOptions() , and I'm looking for something where I can put some initialization code that needs to be called before viewDidLoad() .

Is there such a place?

It is also valid to call viewDidLoad () from another place. Should it be good or too risky?

+4
source share
4 answers

You are wrong.

Place the NSLog directly below the method heading and you will see that ViewDidLoad is directly called after.

 [self.window addSubview:self.yourViewController.view]; 

So you are either using viewDidLoad, or alternatively, and not very pretty that you could use.

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

It is even called before ViewDidload

+4
source

There

  - loadView() 

Removes to viewDidLoad() and comes with advice to never be called right after that. Here is a link to apple docs.

+1
source

I had a similar problem when it was caused when I added a view controller that I wanted to add to the window using the MainWindow.xib file.

To get around this, I assigned the rootViewController window (you can also call addSubView, but it's better to assign rootViewController) in the didFinishLaunchingWithOptions: method of the application delegate. Once you do this, you can easily put any logic that you need, in front or behind where this happens. It allows you full control when your controller boots up. On the contrary, when the view controller is loaded through the tip, it is difficult for him to execute the code in front of him (if at all possible). I know that you specify the main xib in the application, but I do not know if there is a way to run the code before nib loads.

In general, I avoid adding a view controller to xib for this reason.

My code is more like:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // special pre load logic here... UIViewController *myVC = [[MyAwesomeViewController alloc] init]; self.window.rootViewController = myVC; [myVC release]; // special post load logic here... [self.window makeKeyAndVisible]; return YES; } 
0
source

You can put the initialization code in the init method for the class.

And it's ok to call viewDidLoad from another place again. This is just like any other method.

EDIT:

It's nice to call viewDidLoad - but you have to be careful with memory management. If you viewDidLoad objects in viewDidLoad , calling it again will cause leaks. Thus, due to the typical functionality of viewDidLoad you may need to output the code to another method, which you will call again and call from viewDidLoad .

-2
source

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


All Articles