Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method called in -viewWillLayoutSubviews inexplicably runs twice

I've got the following code in a detail view controller:

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    [self configureView];
}

When I segue to this view controller with a breakpoint on first line of -configureView, it appears that [self configureView] is called twice. However:

  • Both times the method is hit, the stack looks like this:

stack

  • When I comment [self configureView] in the above code, it's hit zero times.
  • As evident by the stack being the same both times, -configureView does not call itself recursively.
  • -configureView is only called in the above code.
like image 466
Dmitry Minkovsky Avatar asked Feb 18 '13 22:02

Dmitry Minkovsky


1 Answers

From Apple's documentation:

The viewWillLayoutSubviews method is also called after the view is resized and positioned by its parent.

Given viewWillLayoutSubviews is called whenever the bounds change on the controller's view, there's no guarantee that it'll be invoked once only. It'll be called whenever rotation occurs for example.

Your configureView method is probably better called from somewhere else, perhaps in viewWillAppear, viewDidAppear or even a custom mutator for BirdDetail item as per Hermann's suggestion.

like image 153
followben Avatar answered Oct 02 '22 03:10

followben