View Controller学习笔记—第一章Overview

各位iOS开发大佬们好:
我是一名Swift+SwiftUI栈的iOS小白,目前还在上大三,最近准备实习,面试的过程中发现现在大公司很多还在用OC + UIKit的技术栈,OC我还在考虑要不要学,目前想先把UIKit学完,这是我在官网学习UIKit英文文档时摘录的本人认为的重点,如果你们也觉得对你们有用的话欢迎持续关注,我大概一天更一节,有事除外。格式什么的我也就不做了,翻译都是我自己翻译的,哪里不对欢迎在评论区指正,感谢各位大佬支持

今天2021年12月11日
这一章叫做Overview

The Role of View Controllers

View controller(以下尊称为VC)是app的基础,每个app都至少有一个VC,VC管理着UI的本身,UI与数据,UI与UI之间的变化

The UIViewController class defines the methods and properties for managing your views, handling events, transitioning from one view controller to another, and coordinating with other parts of your app. You subclass UIViewController (or one of its subclasses) and add the custom code you need to implement your app’s behavior.
UIViewController类定义了管理视图,处理事件,转换,与其他视图协调的方法和属性,通过继承UIViewController类或其子类来自定义实现app需要的行为

There are two types of view controllers:
Content view controllers manage a discrete piece of your app’s content and are the main type of view controller that you create.
Container view controllers collect information from other view controllers (known as child view controllers) and present it in a way that facilitates navigation or presents the content of those view controllers differently.
Most apps are a mixture of both types of view controllers.
两类VC:
内容VC管理app中的独立内容
容器VC从其他VC(一般是子VC)收集信息,并以方便导航或以不同方式显示这些视图控制器内容的方式呈现信息。

VC最重要的职责就是管理视图层级
在这里插入图片描述

It is common practice to use outlets to access other views in your view controller’s view hierarchy. Because a view controller manages the content of all its views, an outlet is a property of an object that references another object. outlets let you store references to the views that you need.
通过outlets可以在视图层级中访问其他视图,outlets就是一个储存了其他对象引用的对象属性

A content view controller manages all of its views by itself. A container view controller manages its own views plus the root views from one or more of its child view controllers. The container does not manage the content of its children. It manages only the root view, sizing and placing it according to the container’s design.
内容VC管理了它视图层级下的所有视图,包括其子VC下的所有视图,容器VC就不鸟它的子VC了,根据设计设置尺寸和位置
在这里插入图片描述

A view controller acts as an intermediary between the views it manages and the data of your app. The methods and properties of the UIViewController class let you manage the visual presentation of your app. When you subclass UIViewController, you add any variables you need to manage your data in your subclass. Adding custom variables creates a relationship like the one in Figure 1-3, where the view controller has references to your data and to the views used to present that data. Moving data back and forth between the two is your responsibility
VC是UI和数据之间的桥梁,在VC中处理数据和显示方式,
You should always maintain a clean separation of responsibilities within your view controllers and data objects. Most of the logic for ensuring the integrity of your data structures belongs in the data objects themselves. The view controller might validate input coming from views and then package that input in the format that your data objects require, but you should minimize the view controller’s role in managing the actual data.
你需要划清VC和数据对象之间的责任界限,确保数据结构完整性的大多数逻辑都属于数据对象本身,尽量不要让VC处理实际数据

A UIDocument object is one way to manage your data separately from your view controllers. A document object is a controller object that knows how to read and write data to persistent storage. When you subclass, you add whatever logic and methods you need to extract that data and pass it to a view controller or other parts of your app. The view controller might store a copy of any data it receives to make it easier to update views, but the document still owns the true data.
UIDocument对象是独立于VC管理数据的好东西,它知道如何从持久化存储中读取数据,继承时可以添加一些你需要的方法取出数据并将其传给VC或app的其他部分使用,VC会拿到值后保存一份copy,document仍旧持有原值

View controllers are responder objects and are capable of handling events that come down the responder chain. Although they are able to do so, view controllers rarely handle touch events directly. Instead, views usually handle their own touch events and report the results to a method of an associated delegate or target object, which is usually the view controller. So most events in a view controller are handled using delegate methods or action methods.
VC不直接处理触摸事件,一般是view响应之后将结果传给他们关联的代理或目标对象,而这些关联的代理或目标对象基本上就是VC

The UIViewController class handles most aspects of view management automatically. For example, UIKit automatically releases any view-related resources that are no longer needed. In your UIViewController subclasses, you are responsible for managing any objects you create explicitly.
When the available free memory is running low, UIKit asks apps to free up any resources that they no longer need. One way it does this is by calling the didReceiveMemoryWarning method of your view controllers. Use that method to remove references to objects that you no longer need or can recreate easily later.
UIViewController类管的很宽,比如UIKit会自动释放掉和所有不再需要的视图有关的资源,如果你继承了子类,那么你也需要显式的管的和它一样宽。
如果内存不太够了,UIKit会通过调用VC的 didReceiveMemoryWarning 方法释放掉所有不再需要的或那些可以很轻松重新创建的资源

Adaptivity
View controllers are responsible for the presentation of their views and for adapting that presentation to match the underlying environment. Every iOS app should be able to run on iPad and on several different sizes of iPhone. Rather than provide different view controllers and view hierarchies for each device, it is simpler to use a single view controller that adapts its views to the changing space requirements.
In iOS, view controllers need to handle coarse-grained changes and fine-grained changes. Coarse-grained changes happen when a view controller’s traits change. Traits are attributes that describe the overall environment, such as the display scale. Two of the most important traits are the view controller’s horizontal and vertical size classes, which indicate how much space the view controller has in the given dimension. You can use size class changes to change the way you lay out your views, as shown in Figure 1-4. When the horizontal size class is regular, the view controller takes advantage of the extra horizontal space to arrange its content. When the horizontal size class is compact, the view controller arranges its content vertically.

在这里插入图片描述

Within a given size class, it is possible for more fine-grained size changes to occur at any time. When the user rotates an iPhone from portrait to landscape, the size class might not change but the screen dimensions usually change. When you use Auto Layout, UIKit automatically adjusts the size and position of views to match the new dimensions. View controllers can make additional adjustments as needed.
适配性
VC需要管理app在不同尺寸型号设备上的显示情况,还包括横竖屏,可显示的区域等等

The View Controller Hierarchy

Maintaining the proper view controller relationships ensures that automatic behaviors are delivered to the correct view controllers when they are needed. If you break the prescribed containment and presentation relationships, portions of your app will stop behaving as expected.
VC之间的关系定义了每个VC需要的行为,如果你破坏了预先设置好的关系,那么将造成一些不好的影响
根视图的VC是这个VC层级的中心,每个Window都有且仅有一个填满整个window的根视图VC,根视图VC定义了用户所见的初始内容
在这里插入图片描述

通过UIWindow对象的rootViewController 属性设置根视图VC,SB中会自动设置,但我不是SB哈哈哈,开玩笑,我不用StoryBorad,代码方式必须手动设置
容器VC,例如UINavigationController对象会将它的子VC中的内容和它的navigation bar和可选的toolbar一起显示,还有一些比如 UINavigationController, UISplitViewController, 和 UIPageViewController.
容器类一般就是window的根VC,他们也可以被当作子VC,都行,他们负责布局子VC,子VC一般不需要它的上峰是谁(这两天吃饭时间看新世界,看了两集觉得是个神剧,真狗血,上峰的上峰是上峰的爸爸)

在这里插入图片描述

When container view controllers are involved, UIKit may modify the presentation chain to simplify the code you have to write. Different presentation styles have different rules for how they appear onscreen—for example, a full-screen presentation always covers the entire screen. When you present a view controller, UIKit looks for a view controller that provides a suitable context for the presentation. In many cases, UIKit chooses the nearest container view controller but it might also choose the window’s root view controller. In some cases, you can also tell UIKit which view controller defines the presentation context and should handle the presentation.
替换视图时,UIKit会将旧VC和新VC连起来,替换的时候会匹配新旧,比如全屏替换,则新VC必须也得是可以覆盖全屏或者一个window的根VC,UIKit会提供一个合适的上下文,你可以配置哪个VC提供上下文并且管理替换

在这里插入图片描述
在这里插入图片描述

Design Tips

Use System-Supplied View Controllers Whenever Possible
官方肯定还是希望用自己家的东西,为了阻止你自定义乱七八糟的东西,Apple说了,在你自定义之前,学习一下底下这些东西
The UIKit framework provides view controllers for displaying alerts, taking pictures and video, and managing files on iCloud. UIKit also defines many standard container view controllers that you can use to organize your content.
The GameKit framework provides view controllers for matching players and for managing leaderboards, achievements, and other game features.
The Address Book UI framework provides view controllers for displaying and picking contact information.
The MediaPlayer framework provides view controllers for playing and managing video, and for choosing media assets from the user’s library.
The EventKit UI framework provides view controllers for displaying and editing the user’s calendar data.
The GLKit framework provides a view controller for managing an OpenGL rendering surface.
The Multipeer Connectivity framework provides view controllers for detecting other users and inviting them to connect.
The Message UI framework provides view controllers for composing emails and SMS messages.
The PassKit framework provides view controllers for displaying passes and adding them to Passbook.
The Social framework provides view controllers for composing messages for Twitter, Facebook, and other social media sites.
The AVFoundation framework provides a view controller for displaying media assets.

学完你肯定能(已)找(经)到(吐)能(血)用(了)的

Never modify the view hierarchy of system-provided view controllers. Each view controller owns its view hierarchy and is responsible for maintaining the integrity of that hierarchy. Making changes might introduce bugs into your code or prevent the owning view controller from operating correctly. In the case of system view controllers, always rely on the publicly available methods and properties of the view controller to make all modifications
不仅如此你还不能动人家VC系统的视图层级,它依赖于公开的方法和属性做所有的修改

In cases where two view controllers need to communicate or pass data back and forth, they should always do so using explicitly defined public interfaces.
尽可能让每个VC独立,少知道其他人的情况,做好自己的事,如果两个VC需要联系,他们应该用显式定义好的公共接口
The delegation design pattern is frequently used to manage communication between view controllers. With delegation, one object defines a protocol for communicating with an associated delegate object, which is any object that conforms to the protocol. The exact type of the delegate object is unimportant. All that matters is that it implements the methods of the protocol.
代理模式在两个VC之间联系时非常常用,一个对象定义了一个与它关联的代理对象的协议,另一个对象遵守协议,代理对象的类型不重要,重要的是它得实现协议的方法

Use the Root View Only as a Container for Other Views
Use the root view of your view controller solely as a container for the rest of your content. Using the root view as a container gives all of your views a common parent view, which makes many layout operations simpler. Many Auto Layout constraints require a common parent view to lay out the views properly.
可以用根视图仅作为作为其他内容的一个容器,许多自动布局约束需要公共父视图才能正确布局视图。

Know Where Your Data Lives
In the model-view-controller design pattern, a view controller’s role is to facilitate the movement of data between your model objects and your view objects. A view controller might store some data in temporary variables and perform some validation, but its main responsibility is to ensure that its views contain accurate information. Your data objects are responsible for managing the actual data and for ensuring the overall integrity of that data.
MVC模式下VC的一个职责就是明确数据在V和M之间的移动,VC可能会存储一些临时变量,M的职责为管理实际数据,确保这些数据的实体完整性。
Adapt to Changes
适应不同环境,包括设备尺寸,横竖屏,分屏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值