Simple question viewDidLoad

I have an application with a UINavigationController with a tabBarController and multiple views.

My two main views, which correspond to two tabs, show that when loading data, a good MBProgressHUD . This is triggered by viewDidLoad and is for visibility only. Subsequent updates from the server do not use MBProgressHUD .

The problem is that viewDidLoad is called again in certain situations. I believe this is because the view is unloaded due to memory limitations and then reloaded later, which runs the code that I want to run only for the first time.

My question is: how can I make sure that this is only called when the view is first loaded, without trying to store a temporary account somewhere. All my decisions seem awful hacks. :)

Thanks!

+4
source share
2 answers

in the field of view of the controller:

 @property (nonatomic, assign) BOOL loadStuff; 

in init:

 self.loadStuff = YES; 

in viewDidLoad:

 if (self.loadStuff) { // load stuff here } self.loadStuff = NO; 
+2
source

Only set the HUD if you are actually executing code that takes some time. Then everything will work. Maybe something like:

 @interface MyViewController : UIViewController { NSData* data; } @end @implementation MyViewController - (void)loadData { // put up HUD // start loading the data, will call loadDataFinished when done } - (void)loadDataFinished { // bring down HUD } - (void)viewDidLoad { [super viewDidLoad]; if (data == nil) { [self loadData]; } } @end 
0
source

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


All Articles