iOS: How to know if viewDidLoad got called?
Asked Answered
R

3

18

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.

Rakehell answered 18/10, 2013 at 21:3 Comment(0)
E
63

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.

Essa answered 18/10, 2013 at 21:5 Comment(0)
F
1

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

Franckot answered 18/10, 2013 at 22:46 Comment(0)
V
1

Here's the pedantic answer to this question. If you want to know when viewDidLoad has been triggered, you have to implement viewDidLoad in your view controller

- (void)viewDidLoad
{
    [super viewDidLoad];

    viewDidLoadCalled = YES; // Not actually the best way to do this...
    // Set up more view properties
}

But as Tommy says, you actually need to use isViewLoaded. This gets around the problem of doing a check like

if (!self.view) {
    // do something
}

which inadvertently loads the view by virtue of asking about it.

Be aware that by the time viewWillAppear: is called, the view will always have loaded. Also, on older (pre-iOS 6 I think) releases, the view can unload and be reloaded many times over a view controller's lifetime. Refer to the very nice Big Nerd Ranch view lifecycle diagram for the old behavior. It's almost the same in iOS 6+, except that the view doesn't unload under low memory conditions and viewDidUnload doesn't get called:

enter image description here

Vladikavkaz answered 29/10, 2013 at 21:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.