【iOS】ViewController生命周期

了解ViewController生命周期之前,我们首先认识一下ViewController的结构


UIViewController的结构

视图控制器(ViewController),管理View的层级结构,其自身内置了一个view,可以将view的Controller理解为一个view的container(容器),ta的主要职责有以下几点:

  • 管理View视图的生命周期
  • 响应用户操作
  • 和App整体交互,视图的切换
  • 作为一个容器管理多个Controller和动画

ViewController自身内置的view(UIViewController Default View)就是self.view,把自定义的view通过addSubView:粘贴在这个默认提供的view(self.view)上,就实现了控制器对全部视图的管理,因此可以把视图控制器理解为一个包含多个视图容器

ViewController的生命周期

重点关注以下方法:

方法名称 职能
init ViewController初始化时的一个重载函数
viewDidLoad ViewController内置的默认view创建时调用
viewWillAppear ViewController管理的所有视图即将出现时调用
viewDidAppear ViewController管理的所有视图已经出现时调用
viewWillDisappear ViewController管理的所有视图即将消失时调用
viewDidDisappear ViewController管理的所有视图已经消失时调用
Dealloc ViewController销毁时调用

下面我们将以上方法在程序中重写,观察各个方法被调用的时机:

objectivec 复制代码
@implementation ViewController

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



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.freshView = [[UIView alloc] init];
    self.freshView.backgroundColor = [UIColor magentaColor];
    self.freshView.frame = CGRectMake(0, 0, 266, 277);
    self.freshView.center = self.view.center;
    [self.view addSubview: self.freshView];
    
    UITapGestureRecognizer* gestureTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(hideViewController)];
    [self.freshView addGestureRecognizer: gestureTapRecognizer];
}

//默认视图及其包含的子视图即将在屏幕中出现
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear: animated];
}    // Called when the view is about to made visible. Default does nothing

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear: animated];
}     // Called when the view has been fully transitioned onto the screen. Default does nothing

//Controller从屏幕中消失不见时(对应一些销毁的逻辑)
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear: animated];
} // Called when the view is dismissed, covered or otherwise hidden. Default does nothing

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear: animated];
}  // Called after the view was dismissed, covered or otherwise hidden. Default does nothing

- (void) dealloc {
    
}

- (void)hideViewController {
    UIViewController* viewController = [[UIViewController alloc] init];
    viewController.view.backgroundColor = [UIColor systemMintColor];
    
    viewController.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController: viewController animated: YES completion: nil];
    
}

@end

在重写的每个方法中都打上断点:

可以看到程序先在viewDidLoad里面加载好控件,

ViewController即将出现时,调用viewWillAppear

ViewController已经出现时,调用viewDidAppear

点击隐藏ViewController事件方法后,

新的viewController即将出现时,调用viewWillDisappear

新的viewController已经出现时,调用viewDidDisAppear

总结

了解了UIViewController的生命周期后,就可以在 合适的时机(重写的方法里) 根据需求自定义想要实现的交互事件、代码逻辑

相关推荐
2501_916008892 小时前
全平台抓包工具,Windows、iPhone、Linux三个平台抓包测试
网络协议·计算机网络·网络安全·ios·adb·https·udp
2501_915918415 小时前
怎么把 Python 写的 Flet 应用打包成 iOS App 并上架 App Store?
android·ios·小程序·https·uni-app·iphone·webview
李游Leo7 小时前
HarmonyOS 7 实战开发 04:适配手机、折叠屏与大屏布局
ios·harmonyos
终端安全笔记7 小时前
iOS 27 强制 TLS 1.2:租赁设备的注册链路会在哪一环断
android·网络·安全·ios·智能手机
黑科技iOS上架7 小时前
iOS深度混淆flutter应用的最佳实践
flutter·ios·混淆·审核·深度混淆
思盛iOS签名上架8 小时前
IOS企业签名作用(核心作用一)
ios·testfight
黑科技iOS上架1 天前
unity3D 应用AppStore4.3被拒怎么自查
ios·审核·unity3d混淆
用户38034165882971 天前
FFmpeg 在 iOS 上的裁剪、集成与包体控制
ios·音视频开发
黑科技iOS上架1 天前
Cocos应用App Store4.3被拒怎么自查
macos·ios·objective-c·cocoa·审核·ios混淆·cocos混淆
00后程序员张1 天前
怎么用 Egret(白鹭引擎)打包 iOS 应用并上架 App Store?
android·ios·小程序·https·uni-app·iphone·webview