本文主要是介绍iOS开发-ViewController的生命周期相关,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- ViewController生命周期
- 加载流程
- didReceiveMemoryWarning
- View的layoutSubviews
- Runloop相关
- view的drawRect:方法
ViewController生命周期
加载流程
1.init或者initWithCoder:(NSCoder *)aDecoder:(如果使用storyboard或者xib)
2.loadView:加载view
3.viewDidLoad:view加载完毕
4.viewWillAppear:控制器的view将要显示
5.viewWillLayoutSubviews:控制器的view将要布局子控件
6.viewDidLayoutSubviews:控制器的view布局子控件完成
这期间系统可能会多次调用viewWillLayoutSubviews、viewDidLayoutSubviews俩个方法7.viewDidAppear:控制器的view完全显示
8.viewWillDisappear:控制器的view即将消失的时候
这期间系统也会调用viewWillLayoutSubviews 、viewDidLayoutSubviews 两个方法9.viewDidDisappear:控制器的view完全消失的时候
didReceiveMemoryWarning
Discussion Your app never calls this method directly. Instead, this
method is called when the system determines that the amount of
available memory is low.You can override this method to release any additional memory used by
your view controller. If you do, your implementation of this method
must call the super implementation at some point.
当app收到内存警告的时候会发消息给视图控制器。
app从来不会直接调用这个方法,而是当系统确定可用内存不足的时候采取调用。
如果你想覆写这个方法来释放一些控制器使用的额外内存,你应该在你的实现方法中调用父类的实现方法。
参考文章
https://blog.csdn.net/wangyanchang21/article/details/50730902
https://developer.apple.com/documentation/uikit/uiviewcontroller/1621409-didreceivememorywarning?language=objc
View的layoutSubviews
init
初始化不会触发layoutSubviews
。addSubview
会触发layoutSubviews
。- 改变一个
UIView
的frame
会触发layoutSubviews
,当然前提是frame
的值设置前后发生了变化。 - 滚动一个
UIScrollView
引发UIView
的重新布局会触发layoutSubviews
。 - 旋转Screen会触发父
UIView
上的layoutSubviews
事件。 - 直接调用
setNeedsLayout
或者layoutIfNeeded
。
Runloop相关
在非主页面加载时
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1* frame #0: 0x0000000100529264 GSWatermarkView`-[GSWaterMarkView layoutSubviews](self=0x000000011d801410, _cmd="layoutSubviews") at GSWaterMarkView.m:110frame #1: 0x00000001a77db5b0 UIKitCore`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2156frame #2: 0x00000001a2fe7af0 libobjc.A.dylib`-[NSObject performSelector:withObject:] + 68frame #3: 0x00000001a9d81c0c QuartzCore`-[CALayer layoutSublayers] + 292frame #4: 0x00000001a9d81f14 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 484frame #5: 0x00000001a9d953fc QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 140frame #6: 0x00000001a9cda184 QuartzCore`CA::Context::commit_transaction(CA::Transaction*, double) + 296frame #7: 0x00000001a9d05228 QuartzCore`CA::Transaction::commit() + 684frame #8: 0x00000001a7362d6c UIKitCore`_afterCACommitHandler + 144frame #9: 0x00000001a324bf5c CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 36frame #10: 0x00000001a3246bfc CoreFoundation`__CFRunLoopDoObservers + 420frame #11: 0x00000001a32471ac CoreFoundation`__CFRunLoopRun + 1292frame #12: 0x00000001a3246978 CoreFoundation`CFRunLoopRunSpecific + 480frame #13: 0x00000001ad376534 GraphicsServices`GSEventRunModal + 108frame #14: 0x00000001a7338f0c UIKitCore`UIApplicationMain + 1940frame #15: 0x000000010052814c GSWatermarkView`main(argc=1, argv=0x000000016f8df940) at main.m:14frame #16: 0x00000001a30c6f04 libdyld.dylib`start + 4
在初始界面加载时
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1* frame #0: 0x0000000100b05264 GSWatermarkView`-[GSWaterMarkView layoutSubviews](self=0x000000010130f210, _cmd="layoutSubviews") at GSWaterMarkView.m:110frame #1: 0x00000001a77db5b0 UIKitCore`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2156frame #2: 0x00000001a2fe7af0 libobjc.A.dylib`-[NSObject performSelector:withObject:] + 68frame #3: 0x00000001a9d81c0c QuartzCore`-[CALayer layoutSublayers] + 292frame #4: 0x00000001a9d81f14 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 484frame #5: 0x00000001a9d953fc QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 140frame #6: 0x00000001a9cda184 QuartzCore`CA::Context::commit_transaction(CA::Transaction*, double) + 296frame #7: 0x00000001a9d05228 QuartzCore`CA::Transaction::commit() + 684frame #8: 0x00000001a7350d20 UIKitCore`__34-[UIApplication _firstCommitBlock]_block_invoke_2 + 84frame #9: 0x00000001a324c95c CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 28frame #10: 0x00000001a324c0e0 CoreFoundation`__CFRunLoopDoBlocks + 268frame #11: 0x00000001a32470e0 CoreFoundation`__CFRunLoopRun + 1088frame #12: 0x00000001a3246978 CoreFoundation`CFRunLoopRunSpecific + 480frame #13: 0x00000001ad376534 GraphicsServices`GSEventRunModal + 108frame #14: 0x00000001a7338f0c UIKitCore`UIApplicationMain + 1940frame #15: 0x0000000100b0414c GSWatermarkView`main(argc=1, argv=0x000000016f303940) at main.m:14frame #16: 0x00000001a30c6f04 libdyld.dylib`start + 4
可以看出在app启动时,初始界面View的layoutSubviews
由__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__
触发,后续的界面由__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__
触发
view的drawRect:方法
- 直接调用
setNeedsDisplay
,或者setNeedsDisplayInRect:
触发drawRect:
,但是有个前提条件是rect
大小不能为0。 drawRect
的调用时机是在viewWillAppear
和viewDidAppear
之间。且在View
的layoutSubviews
之后
2020-03-03 14:05:58.384185+0800 GSWatermarkView[1318:1216023] ViewController [-[ViewController loadView]]
2020-03-03 14:05:58.384252+0800 GSWatermarkView[1318:1216023] ViewController [-[ViewController viewDidLoad]]
2020-03-03 14:05:58.385593+0800 GSWatermarkView[1318:1216023] GSWaterMarkView [-[GSWaterMarkView didMoveToSuperview]]
2020-03-03 14:05:58.385686+0800 GSWatermarkView[1318:1216023] ViewController [-[ViewController viewWillAppear:]]
2020-03-03 14:05:58.387915+0800 GSWatermarkView[1318:1216023] ViewController [-[ViewController viewWillLayoutSubviews]]
2020-03-03 14:05:58.387956+0800 GSWatermarkView[1318:1216023] ViewController [-[ViewController viewDidLayoutSubviews]]
2020-03-03 14:05:58.387975+0800 GSWatermarkView[1318:1216023] GSWaterMarkView [-[GSWaterMarkView layoutSubviews]]
2020-03-03 14:05:58.388046+0800 GSWatermarkView[1318:1216023] GSWaterMarkView [-[GSWaterMarkView drawRect:]]
2020-03-03 14:05:58.427785+0800 GSWatermarkView[1318:1216023] ViewController [-[ViewController viewDidAppear:]]
- 调用
sizeToFit
,会触发drawRect
的调用。 - 通过设置
contentMode
属性值为UIViewContentModeRedraw
。那么将在每次设置或更改frame
的时候自动调用drawRect:
。
app启动时,添加到初始界面的堆栈,还是CALayer
触发
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1* frame #0: 0x00000001042e531c GSWatermarkView`-[GSWaterMarkView drawRect:](self=0x0000000104910450, _cmd="drawRect:", rect=(origin = (x = 0, y = -0.125), size = (width = 374, height = 210.5))) at GSWaterMarkView.m:115frame #1: 0x00000001a77da8c0 UIKitCore`-[UIView(CALayerDelegate) drawLayer:inContext:] + 608frame #2: 0x00000001a9d83d78 QuartzCore`-[CALayer drawInContext:] + 312frame #3: 0x00000001a9c4f9e8 QuartzCore`CABackingStoreUpdate_ + 180frame #4: 0x00000001a9d84ce0 QuartzCore`___ZN2CA5Layer8display_Ev_block_invoke + 56frame #5: 0x00000001a9cd6f64 QuartzCore`x_blame_allocations + 148frame #6: 0x00000001a9d847b0 QuartzCore`-[CALayer _display] + 1652frame #7: 0x00000001a9d9551c QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 428frame #8: 0x00000001a9cda184 QuartzCore`CA::Context::commit_transaction(CA::Transaction*, double) + 296frame #9: 0x00000001a9d05228 QuartzCore`CA::Transaction::commit() + 684frame #10: 0x00000001a7350d20 UIKitCore`__34-[UIApplication _firstCommitBlock]_block_invoke_2 + 84frame #11: 0x00000001a324c95c CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 28frame #12: 0x00000001a324c0e0 CoreFoundation`__CFRunLoopDoBlocks + 268frame #13: 0x00000001a32470e0 CoreFoundation`__CFRunLoopRun + 1088frame #14: 0x00000001a3246978 CoreFoundation`CFRunLoopRunSpecific + 480frame #15: 0x00000001ad376534 GraphicsServices`GSEventRunModal + 108frame #16: 0x00000001a7338f0c UIKitCore`UIApplicationMain + 1940frame #17: 0x00000001042e414c GSWatermarkView`main(argc=1, argv=0x000000016bb23940) at main.m:14frame #18: 0x00000001a30c6f04 libdyld.dylib`start + 4
参考文章
https://segmentfault.com/a/1190000018645601
https://www.jianshu.com/p/4edfd23ca49a
这篇关于iOS开发-ViewController的生命周期相关的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!