yaofly2012 / note

Personal blog
https://github.com/yaofly2012/note/issues
44 stars 5 forks source link

UIViewController #279

Open yaofly2012 opened 1 year ago

yaofly2012 commented 1 year ago

UIViewController

iPhone用户看到的App页面都是UIViewControllerUIViewController是管理UIKit应用程序视图层次结构。 在MVC设计模式扮演控制层的角色。

image

UIViewController作为Controller主要职责有:

  1. 视图(UIView)管理
  2. 处理用户交互

视图管理

每个ViewController管理一个视图层级(View Hierarchy)

根视图(Root View)

根视图是管理视图层级的一个容器。

指定视图(View)

有三种方式指定视图:

  1. storyboard
  2. xib (nib)文件
  3. 纯代码

storyboard和xib文件方式属于Interface Builder

View生命周期

image

创建View的过程

image

(摘自iOS开发-UIViewController使用详解

生命周期

image

摘自UIViewController Lifecycle

1. 创建阶段

2. 加载

注意:

  1. 只创建ViewController实例对象并不会触发回调,只有当ViewController实例对象首次被添加到视图里才会触发回调。

3. 展示阶段

image

创建UIViewController

基于三种指定视图的方式,创建UIViewController对应有三种方式:

  1. storyboard
    
    // 先加载storyboard文件
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"文件名" bundle:nil];

// 接着初始化storyboard中的控制器 // 初始化“初始控制器”(storyboard里箭头所指的控制器) ViewController *vc = [storyboard instantiateInitialViewController];

// 通过一个标识初始化对应的控制器 // ViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"标识"];


2. xib (nib)文件
```Objective-C
UIViewController *vc = [UIViewController alloc] initWithNibName:@"文件名" bundle:(nil默认mainbundle)];
  1. 直接通过代码
    UIViewController *vc = [UIViewController alloc] init];

    init方法内部也会调用initWithNibName

参考

  1. UIViewController使用详解
  2. iOS开发-UIViewController使用详解
  3. Understanding the UIViewController Lifecycle in iOS
  4. UIViewController Lifecycle - 分类
  5. UIViewController 生命周期-执行顺序
  6. 深入讲解iOS开发中的UIViewController
yaofly2012 commented 1 year ago

视图UIView

UIViewControllerUIView关系:

VC.view属性表示根视图。

loadView

在实例化UIViewController时会创建一个UIView对象作为UIViewController的root视图(view属性)。loadView方法就是用于创建root视图的,但是开发者不要直接调用改方法,系统会在“适当的”时候自动调用该方法。即在访问view属性时如果view属性是nil,则系统会自动调用loadView创建一个有效的root视图。

参考

  1. loadView()
  2. loadView学习总结
  3. Guide to Creating UIViewController Without Storyboard
  4. How to create a new Xcode project without Storyboard
  5. 知乎:iOS 开发不用 Storyboard 不行吗?
yaofly2012 commented 1 year ago

Storyboard创建视图

概念术语

名称 解释
Inspectors
First Responder
Exit
Initial View Controller
Outlet Outlet
Segue
Scene

基础

手动关联UIViewController

给storyboard里添加的ViewController手动关联UIViewController

image
  1. 选择storyboard里的ViewController控件;
  2. 在Inspectors面板里切换到【Identity inspector】Tab;
  3. 在【Custom class】里选择指定的UIViewController即可。

手动添加Storyboard文件

    // 创建storyboard对象
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"SubView" bundle:nil];

    // 通过storyboard对象创建控制器
    // instantiateInitialViewController:加载storyboard箭头指向的控制器
    UIViewController *vc = [storyboard instantiateInitialViewController];
    [self presentViewController:vc animated:YES completion:^{
        NSLog(@"Did presentViewController");
    }];

Application tried to present a nil modal view controller on target “Current View Controller”解决方案

Segue

Storyboard里多个UIViewControllerView协作。 在Storyboard里就是这个连接UIViewControllerView的箭头:

image

利用Segue的唯一标识符访问Segue。

参考

  1. Xcode12在storyboard添加组件和事件,添加新页面及跳转
  2. 四、UI开发之核心基础——约束(实用)
  3. 不错的IOS学习笔记
  4. Application tried to present a nil modal view controller on target “Current View Controller”解决方案
yaofly2012 commented 1 year ago

纯代码创建视图

[译]纯代码创建 UIView