当视图即将显示在屏幕上时,会在 UIViewController 上调用 viewWillAppear。是否有可能在 UIView 上获得类似的回调?
问问题
19985 次
3 回答
50
UIView参考中的以下内容怎么样
willMoveToSuperview:, didMoveToSuperview - 根据需要实现这些方法以跟踪当前视图在视图层次结构中的移动。
这至少会告诉您何时将其添加到视图层次结构中,但不能保证视图层次结构已呈现/可见。
于 2012-05-02T13:47:37.837 回答
10
不,但是您可以将该通知从您的视图控制器转发到所需的视图。
于 2012-05-02T12:53:35.050 回答
-1
- 请注意,我最初打算在此处发布此答案,但由于几年前有人问过此问题,因此我将其标记为重复,现在将在此处回答。
宏观解决方案
我为此制定了一个非常优雅且易于使用的宏解决方案。
.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
于 2018-11-15T00:34:02.413 回答