1

I have a view that exits in its own class, with its own xib.

This view is initialized and added as a subview to my viewController view.

When the view is initialized, the method layoutSubviews is called where i customize some stuff in the view.

BUT which method is called when the view is removed from the superview (if any)?

For example, for a ViewController, viewWill/DidDisappear is called. Is there a similar method to a UIView (opposite to the layoutSubviews)?

Thanks in advance

---EDIT---

I just found a method that is called both on adding and removing a subView:

- (void)willMoveToSuperview:(UIView *)newSuperview

AND if newSuperview == 0, you can customize the removing of the subview.

Am i right or is it a tacky way to handle the situation?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
B-Man
  • 2,209
  • 3
  • 22
  • 35
  • I imagine you have found the best solution, my question though is why do you need to know when a view is removed from a layout? – Dan F Jul 02 '13 at 20:33
  • "didMoveToWindow" appeared to be what you are looking for. This occurs when the receiver has just been removed from its superview or when the receiver has just been added to a superview that is not attached to a window. Overrides of this method may choose to ignore such cases if they are not of interest. – ldindu Jul 02 '13 at 20:34
  • @DanF Thanks for your comment. I need it because i am kind of using it instead of a viewcontroller. it is a small view, and i thought it would be lighter. – B-Man Jul 02 '13 at 20:44
  • @ldindu thanks for your comment. didMoveToWindow is called also on adding the subview. i am only interested in "removing" the subview. the willMoveToSuperview includes the newSuperView parameter which in this case is helpful :) – B-Man Jul 02 '13 at 20:46

1 Answers1

3

BUT which method is called when the view is removed from the superview (if any)?

-removeFromSuperview is called, so you can override that if you need to do some housekeeping when the view is removed. Just remember to call super's version, too.

-layoutSubviews isn't necessarily only called when the view is added to a superview -- it's called whenever layout is needed. For example, it might be called when the orientation changes, or when the superview lays itself out again, or when the view's frame changes. There's really not an inverse of -layoutSubviews because none is needed. (What would it be called? -messupSubviews? ;-))

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • Overriding the removeFromSuperview is the best solution i think! Thanks! I am using a uiview subclass as a "lighter" viewcontroller. – B-Man Jul 02 '13 at 20:47
  • and BTW i do understand the function of the layoutSubviews, i am using it to customize the view when added. is there a better way? – B-Man Jul 02 '13 at 20:50