惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
About on SuperTechFans
小众软件
小众软件
WordPress大学
WordPress大学
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements
博客园 - 三生石上(FineUI控件)
博客园_首页
N
Netflix TechBlog - Medium
IT之家
IT之家
H
Help Net Security
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
罗磊的独立博客
T
Tailwind CSS Blog
F
Fortinet All Blogs
Hugging Face - Blog
Hugging Face - Blog
MongoDB | Blog
MongoDB | Blog
V
V2EX
量子位
云风的 BLOG
云风的 BLOG
爱范儿
爱范儿
博客园 - 司徒正美
The Cloudflare Blog
Engineering at Meta
Engineering at Meta

博客园 - 观海云不远

ChatView:把 ChatGPT / Claude 对话保存、分享和发布成独立网页 AI对普通人到底有什么用?这8个场景告诉你答案 用 AI 做点不一样的事:你出想法,我来实现 Python 调试工具PDB的基本使用 帮园子算账省钱 linux I/O性能优化 Linux内存问题排查工具 CPU优化方案 系统中出现大量不可中断进程和僵尸进程怎么办? CPU 使用率过高怎么办 CPU上下文切换 到底应该怎么理解“平均负载”? iOS开发学习笔记(OC语言)——block使用示例 iOS开发学习笔记(OC语言)——调起APP(URL Scheme) iOS开发学习笔记(OC语言)——文件基本操作 iOS开发学习笔记(OC语言)——网络请求 iOS开发学习笔记(OC语言)——底部tab栏 iOS开发学习笔记(OC语言)——线程安全的单例 Jenkins安装配置回滚 Mac 下反编译Android APK
iOS开发学习笔记(OC语言)——UIView和UIViewController生命周期
观海云不远 · 2022-01-28 · via 博客园 - 观海云不远

UIView 生命周期


#import "ViewController.h"

@interface TestView: UIView

@end

@implementation TestView

- (instancetype)init{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)willMoveToSuperview:(nullable UIView *)newSuperview {
    [super willMoveToSuperview:newSuperview];
}

- (void)didMoveToSuperview {
    [super didMoveToSuperview];
}

- (void)willMoveToWindow:(nullable UIWindow *)newWindow {
    [super willMoveToWindow:newWindow];
}

- (void)didMoveToWindow {
    [super didMoveToWindow];
}

@end


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    TestView *view = [[TestView alloc] init];
    view.backgroundColor = [UIColor redColor];
    view.frame = CGRectMake(100, 100, 100, 100);
    [self.view addSubview:view];
}

@end

通过断点调试,可以发现生命周期是:

  1. init
  2. willMoveToSuperview
  3. didMoveToSuperview
  4. willMoveToWindow
  5. didMoveToWindow

UIViewController 生命周期

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (instancetype)init {
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor redColor];
    view.frame = CGRectMake(100, 100, 100, 100);
    [self.view addSubview:view];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}


@end

通过断点调试,可以发现生命周期是:

  1. init
  2. viewDidLoad
  3. viewDidAppear

如果移除,顺序是:

  1. viewWillDisappear
  2. viewDidDisappear