didAddSubview:、willRemoveSubview:、willMoveToSuperview:、didMoveToSuperview、willMoveToWindow

// 当视图添加子视图时调用

- (void)didAddSubview:(UIView *)subview;


// 当子视图从本视图移除时调用

- (void)willRemoveSubview:(UIView *)subview;


// 当视图即将加入父视图时 / 当视图即将从父视图移除时调用

- (void)willMoveToSuperview:(nullable UIView *)newSuperview;


// 当试图加入父视图时 / 当视图从父视图移除时调用

- (void)didMoveToSuperview;


// 当视图即将加入父视图时 / 当视图即将从父视图移除时调用

- (void)willMoveToWindow:(nullable UIWindow *)newWindow;


// 当视图加入父视图时 / 当视图从父视图移除时调用

- (void)didMoveToWindow;



1. 

MyView *view = [[MyView alloc] initWithFrame:CGRectMake(10, 30, 300, 300)];

view.tag = 100;

view.backgroundColor = [UIColor redColor];

[self.view addSubview:view];

UIView *v = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];

v.backgroundColor = [UIColor yellowColor];

[view addSubview:v];


2016-05-10 11:02:46.737 Test[2489:101281] willMoveToSuperview:

2016-05-10 11:02:46.737 Test[2489:101281] didMoveToSuperview

2016-05-10 11:02:46.738 Test[2489:101281] didAddSubview:

2016-05-10 11:02:46.738 Test[2489:101281] willMoveToWindow:

2016-05-10 11:02:46.738 Test[2489:101281] didMoveToWindow



2. 

MyView *myView = [(MyView *)self.view viewWithTag:100];

[myView removeFromSuperview];


2016-05-10 11:02:48.394 Test[2489:101281] willMoveToSuperview:

2016-05-10 11:02:48.395 Test[2489:101281] willMoveToWindow:

2016-05-10 11:02:48.395 Test[2489:101281] didMoveToWindow

2016-05-10 11:02:48.395 Test[2489:101281] didMoveToSuperview

2016-05-10 11:02:48.396 Test[2489:101281] willRemoveSubview:
————————————————
版权声明:本文为CSDN博主「Bill-Lin」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_21649645/article/details/51361467

 

posted @ 2022-08-05 16:48  brave-sailor  阅读(146)  评论(0编辑  收藏  举报