什么类似的功能方法(viewWillAppear中)上存在的UIView?(What similar

2019-07-29 06:03发布

viewWillAppear中被称为上的UIViewController时的图是关于在屏幕上显示。 是否有可能得到类似的UIView回调?

Answer 1:

如何从下面的UIView参考

willMoveToSuperview :, didMoveToSuperview - 根据需要跟踪您的视图层次当前视图的运动实现这些方法。

这至少会告诉你,当它被添加到一个视图层次,但不保证该视图层次呈现/可见。



Answer 2:

没有,但你可以在通知转发给您的视图控制器所需的视图。



Answer 3:

  • 请注意,我本来是要发布这个答案在这里 ,但看到因为这是问前两年我标志着一个为重复的,现在反而会回答这里。

宏解决方案

我做了这个一个宏观的解决方案,这是非常优雅,易于使用。


.M

以下添加到您的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
}

对! 它真的很容易!

(由于怎样的宏ifdef的来回切换是建立在下面,你可以使用一个或另一个或两个!)


。H

以下添加到您的.h下@end (或者,如果你想保持干净的东西,你可以简单地添加到一个名为macros.h和#IMPORT它)

注意:如果添加这个(或进口本)将多个文件也导入对方或@class对方你可以最终搞乱了#ifdef来逻辑和这个宏可能会失败,我建议从独立的.h文件导入每个你需要在课堂上,这个宏被创造更多的概念不是一些的证明在生产代码中使用

//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


文章来源: What similar functionality method (viewWillAppear) exists on UIView?