A Quick tip: Adding and removing NSNotificationCenter observers in UIView subclass

It is sometimes needed for a UIView subclass to register as an NSNotificationCenter observer. However, there seems to be no good place for adding/removing NSNotificationCenter observers… The best place, I have found so far, is in willMoveToWindow: and didMoveToWindow methods.

Just add observers in didMoveToWindow and remove them in willMoveToWindow!




     – is called immediately after its window property gets set.


    – is called before the view is added to to a window. It also will be called when the view is removed from its window. The newWindow property will be nil then. That’s why a check that newWindow is nil needs to be performed (it indicates that the view was just removed).

One Response
  1. consent judgment

Leave a Reply

Your email address will not be published. Required fields are marked *