Search code examples
iosuiviewuiviewcontrolleruikituiwindow

On iOS, in a ViewController's viewDidLoad, the number of subviews is 0? Which method can show the populated number?


Using Xcode 4.3.2, starting a brand new Single View App, in ViewController's

- (void)viewDidLoad
{
    // ...

    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    NSLog(@"How many subviews %i", window.subviews.count);
}

The count is actually 0... if I print out the number in a tap event handler, then I do see 1, which is the main view of the app. So I wonder how come in viewDidLoad, [subviews count] is 0 and is there an init or begin method like viewDidLoad where it would print 1 already (no need to print it in the tap event handler).


Solution

  • Try to NSLog the count of subviews in - (void)viewDidAppear:(BOOL)animated

    viewDidLoad Called after the controller’s view is loaded into memory. viewDidAppear: Notifies the view controller that its view was added to a view hierarchy.