5

If you implement willMoveToSuperview on a UIView subclass, is it guaranteed to be called (with nil) when removeFromSuperview is called on your view?

The docs say that the newSuperview parameter may be nil and that it's called "whenever the superview changes" but I'm not sure if I can interpret this to mean it will be called when the view is removed from its superview even when not being moved to a different superview.

jhabbott
  • 18,461
  • 9
  • 58
  • 95

1 Answers1

15

Whenever a view receives removeFromSuperview, and the view's superview was not already nil, the view will always do [self willMoveToSuperview:nil].

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • Are you assuming that the view is still retained? If its old super view is the sole owner, the view will be gone forever after `removeFromSuperview`, right? – Philip007 Nov 28 '12 at 18:20
  • The `removeFromSuperview` method sends `willMoveToSuperview:` and `didMoveToSuperview` to `self` before finally releasing `self`. If the superview had the only strong reference to the subview, the subview will receive the messages before being deallocated. – rob mayoff Nov 28 '12 at 21:48