Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: How to know if viewDidLoad got called?

Is there a BOOL or some other way of knowing if viewDidLoad: has been called?

I need to change a view property every time my view has entered active, but if the view hasn't ever been loaded, I don't want to prematurely trigger viewDidLoad:. If there isn't way of easily telling if viewDidLoad: has been called, I'll simply add a BOOL called loaded set to NO in the view controller init, then change the view properties after entered active if loaded is YES or in viewWillAppear: if loaded is NO then set loaded to YES.

like image 398
kev Avatar asked Oct 18 '13 21:10

kev


People also ask

How often is viewDidLoad called?

viewDidLoad() is one of the initialization methods that is called on the initial view controller. viewDidLoad() is called before anything is shown to the user - and it is called only once.

Is viewDidLoad called once?

viewDidLoad() Called after init(coder:) when the view is loaded into memory, this method is also called only once during the life of the view controller object. It's a great place to do any view initialization or setup you didn't do in the Storyboard.

Is viewDidAppear called after viewDidLoad?

viewDidLoad is called after your view is loaded. It is called only once when view is initialized and pushed or presented. viewDidAppear is called once you see the loaded view on screen. It is called after view appeared.

What happens before viewDidLoad?

Yes viewdidload: is called before viewWillAppear:. and The apple developer guide says this method gets called after the view is loaded into memory means, The viewController in storyboard/XIB is loading into device memory.


2 Answers

Use isViewLoaded. Other than that it does exactly what you want, there's not that much to say about it. The documentation is as simple as:

Calling this method reports whether the view is loaded. Unlike the view property, it does not attempt to load the view if it is not already in memory.

like image 81
Tommy Avatar answered Sep 17 '22 23:09

Tommy


Perhaps you should init your UIView in viewDidLoad, and then change it in whichever way you need to inside viewWillLayoutSubviews.

like image 33
Andrew Avatar answered Sep 17 '22 23:09

Andrew