IOS UIView的生命周期的实例详解
程序员文章站
2023-12-18 15:33:28
ios uiview的生命周期的实例详解
任何对象的者有一个生命周期,即都存在一个实例化到销毁的过程。
uiview对象也不例外,那么uiview从init/new...
ios uiview的生命周期的实例详解
任何对象的者有一个生命周期,即都存在一个实例化到销毁的过程。
uiview对象也不例外,那么uiview从init/new开始后,直到dealloc结束的过程中都经历了哪些过程呢?
首先自定义继承自uiview的对象lifeview
#import <uikit/uikit.h> @interface lifeview : uiview @end
#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视图控制器中实例化,并添加到父视图
- (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的相关方法,如下所示:
// 实例化时 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的相关方法,如下所示:
// 销毁时 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时,则是被销毁了,即结束生命周期。
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!