IOS UIView的生命周期的实例详解

2025-05-29 0 24

IOS UIView生命周期的实例详解

任何对象的者有一个生命周期,即都存在一个实例化到销毁的过程。

UIView对象也不例外,那么UIView从init/new开始后,直到dealloc结束的过程中都经历了哪些过程呢?

首先自定义继承自UIView的对象LifeView

?

1

2

3

4

5
#import <UIKit/UIKit.h>

@interface LifeView : UIView

@end

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58
#import "LifeView.h"

@interface LifeView ()

{

NSInteger count;

}

@end

@implementation LifeView

- (instancetype)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self)

{

NSLog(@"<-- 1 %s , count = %@-->", __func__, @(count++));

}

return self;

}

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

{

NSLog(@"<-- 2 %s , count = %@-->", __func__, @(count++));

}

- (void)didMoveToSuperview

{

NSLog(@"<-- 3 %s , count = %@-->", __func__, @(count++));

}

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

{

NSLog(@"<-- 4/7 %s , count = %@-->", __func__, @(count++));

}

- (void)didMoveToWindow

{

NSLog(@"<-- 5/8 %s , count = %@-->", __func__, @(count++));

}

- (void)layoutSubviews

{

NSLog(@"<-- 6 %s , count = %@-->", __func__, @(count++));

}

- (void)removeFromSuperview

{

NSLog(@"<-- 9 %s , count = %@-->", __func__, @(count++));

}

- (void)dealloc

{

NSLog(@"<-- 10 %s , count = %@-->", __func__, @(count++));

}

@end

其次,在B视图控制器中实例化,并添加到父视图

?

1

2

3

4

5

6

7

8

9

10

11
- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

self.title = @"life view";

LifeView *lifeView = [[LifeView alloc] initWithFrame:CGRectMake(10.0, 80.0, 100.0, 100.0)];

[self.view addSubview:lifeView];

lifeView.tag = 1000;

lifeView.backgroundColor = [UIColor orangeColor];

}

当B视图控制器被push,或present出来时,被调用的LifeView的相关方法,如下所示:

?

1

2

3

4

5

6

7
// 实例化时

2017-06-16 00:37:10.694 DemoViewLife[3963:121184] <-- 1 -[LifeView initWithFrame:] , count = 0-->

2017-06-16 00:37:10.695 DemoViewLife[3963:121184] <-- 2 -[LifeView willMoveToSuperview:] , count = 1-->

2017-06-16 00:37:10.695 DemoViewLife[3963:121184] <-- 3 -[LifeView didMoveToSuperview] , count = 2-->

2017-06-16 00:37:10.697 DemoViewLife[3963:121184] <-- 4/7 -[LifeView willMoveToWindow:] , count = 3-->

2017-06-16 00:37:10.697 DemoViewLife[3963:121184] <-- 5/8 -[LifeView didMoveToWindow] , count = 4-->

2017-06-16 00:37:10.701 DemoViewLife[3963:121184] <-- 6 -[LifeView layoutSubviews] , count = 5-->

当B视图控制器被pop,或dismiss时,被调用的LifeView的相关方法,如下所示:

?

1

2

3

4

5

6
// 销毁时

2017-06-16 00:37:25.514 DemoViewLife[3963:121184] <-- 4/7 -[LifeView willMoveToWindow:] , count = 6-->

2017-06-16 00:37:25.514 DemoViewLife[3963:121184] <-- 5/8 -[LifeView didMoveToWindow] , count = 7-->

2017-06-16 00:37:25.515 DemoViewLife[3963:121184] <-- 9 -[LifeView removeFromSuperview] , count = 8-->

2017-06-16 00:37:25.516 DemoViewLife[3963:121184] <-- 9 -[LifeView removeFromSuperview] , count = 9-->

2017-06-16 00:37:25.518 DemoViewLife[3963:121184] <-- 10 -[LifeView dealloc] , count = 10-->

从打印信息可以看到方法"removeFromSuperview"被调用了两次,这是因为在B视图控制器中,主动调用了该方法。如果没有主动调用该方法的话,只会被调用一次。

另外被执行过两次的方法还有"willMoveToWindow"和"didMoveToWindow"。我们可以很好地进行区分,当newWindow存在时,则是实例化创建被第一次调用;而当newWindow为nil时,则是被销毁了,即结束生命周期

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 IOS UIView的生命周期的实例详解 https://www.kuaiidc.com/90263.html

相关文章

发表评论
暂无评论