What similar functionality method (viewWillAppear) exists on UIView? What similar functionality method (viewWillAppear) exists on UIView? objective-c objective-c

What similar functionality method (viewWillAppear) exists on UIView?


How about the following from the UIView reference

willMoveToSuperview:, didMoveToSuperview - Implement these methods as needed to track the movement of the current view in your view hierarchy.

this will at least tell you when it is added to a view hierarchy but there is no guarantee that the view hierarchy is presented/viewable.


No, but you can forward that notification to the desired view from your view controller.


  • Note, I was originally going to post this answer here but seeing as this was asked years prior I marked that one as a duplicate and will now answer here instead.

Macro Solution

I've made a macro solution for this that is very elegant and easy to use.


.m

Add the following to your .m

-(void)viewDidLoad {    //normal stuff}__METHOD_MACRO_wasRemovedFromSuperview() {    //Code here will run whenever this view is removed from its superview}__METHOD_MACRO_wasAddedAsSubview() {    //Code here will run whenever this view is added as the subview of another view}

Yup! It really is that easy!

(Due to how the ifdef toggling of the macro is set-up below, you can use one or the other or both!)


.h

Add the following to your .h beneath @end (or, if you want to keep things clean, you can simply add to a file called macros.h and #import it)

Note: If you add this (or import this) to multiple files that also import each other or @class each other you can end up messing up the #ifdef logic and this macro could fail, I suggest importing it from a separate .h file for each class you need it in, this macro was created more as a proof of concept than something to be used in production code

//Logic for __METHOD_MACRO_wasAddedAsSubview() and __METHOD_MACRO_wasRemovedFromSuperview()#define startSuppressingWarnings() \_Pragma("clang clang diagnostic push")\_Pragma("clang diagnostic ignored \"-Weverything\"")#define stopSuppressingWarnings() \_Pragma("clang clang diagnostic pop")#define __INTERNAL_METHOD_MACRO__didChangeSuperviewsSetup() \-(void)didMoveToWindow {\[super didMoveToWindow];\if (self.window && self.superview) {\startSuppressingWarnings()/*Hide potential undeclared selector warnings*/\if ([self respondsToSelector:@selector(__wasAddedAsSubview)]) {\[self performSelector:@selector(__wasAddedAsSubview)];\}\stopSuppressingWarnings()\} else {\startSuppressingWarnings()/*Hide potential undeclared selector warnings*/\if ([self respondsToSelector:@selector(__wasRemovedFromSuperview)]) {\[self performSelector:@selector(__wasRemovedFromSuperview)];\}\stopSuppressingWarnings()\}\}#ifdef __INTERNAL_METHOD_MACRO__didChangeSuperviewsSetup_wasSetup///Called when UIView was added as subview OR moved to another superview OR if another VC was presented#define __METHOD_MACRO_wasAddedAsSubview() \-(void)__wasAddedAsSubview/*{ //perform extra processes here }*/#else#define __INTERNAL_METHOD_MACRO__didChangeSuperviewsSetup_wasSetup 1///Called when UIView was added as subview OR moved to another superview#define __METHOD_MACRO_wasAddedAsSubview() \__INTERNAL_METHOD_MACRO__didChangeSuperviewsSetup() \-(void)__wasAddedAsSubview/*{ //perform extra processes here }*/#endif#ifdef __INTERNAL_METHOD_MACRO__didChangeSuperviewsSetup_wasSetup///Called when UIView is removed as subview from superview or when its parent window is removed#define __METHOD_MACRO_wasRemovedFromSuperview() \-(void)__wasRemovedFromSuperview/*{ //perform extra processes here }*/#else#define __INTERNAL_METHOD_MACRO__didChangeSuperviewsSetup_wasSetup 1///Called when UIView is removed as subview from superview#define __METHOD_MACRO_wasRemovedFromSuperview() \__INTERNAL_METHOD_MACRO__didChangeSuperviewsSetup() \-(void)__wasRemovedFromSuperview/*{ //perform extra processes here }*/#endif