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:



你可能感兴趣的:(iOS)