macOS学习之OS X View Controllers


View Controller Life Cycle


Since view controllers are responsible for managing views, they expose methods that allow you to hook into events associated with the views. For example the point at which the views have loaded from the storyboard, or when the views are about to appear on the screen. This collection of event-based methods are known as the view controller life cycle.
The life cycle of a view controller can be divided into three major parts: its creation, its lifetime, and finally its termination. Each part has methods you can override to do additional work.

翻译:


由于视图控制器负责管理视图,所以它们会暴露允许您与视图关联的事件的方法.。例如,在这一点上视图已经从storyboard中加载,或者视图将出现在屏幕上的时候。这种基于事件的方法的集合被称为视图控制器生命周期
一个视图控制器的生命周期可以分为三个主要部分:它的创建,它的生命时间,并最终终止。每个部分都有方法可以重写做额外的工作.。

Creation

  1. viewDidLoad() is called once the view is fully loaded and can be used to do one-time initializations like the configuration of a number formatter, register for notifications, or API calls that only need to be done once.
  2. viewWillAppear() is called every time the view is about to appear on screen. In our application, it is called every time you select the Overview tab. This is a good point to update your UI or to refresh your data model.
  3. viewDidAppear() is called after the view appears on screen. Here you can start some fancy animations.

翻译:


  1. viewDidLoad() 视图被完全载入的时候只会被调用一次,并且能够被用于一次性的初始化,比如number formatter的配置,注册通知,或者API的调用这些只需做一次的操作
  2. viewWillAppear() 每次视图显示在屏幕中的时候都会被调用。在我们的应用中,当你每次选择Overview tab的时候都会调用它。这是更新UI或刷新数据的好方法。
  3. viewDidAppear() 当视图已经显示到屏幕上的时候会被调用,这里你可以开始一些花式的动画。

Lifetime

Once a view controller has been created, it then enters a period during which it it handles user interactions. It has three methods specific to this phase of its life:

翻译:


一旦创建了一个视图控制器,它就进入一个处理用户交互的周期。它有三个方法具体到这一阶段:

  1. updateViewConstraints() is called every time the layout changes, like when the window is resized.
  2. viewWillLayout() is called before the layout() method of a view controller’s view is called. For example, you can use this method to adjust constraints.
  3. viewDidLayout() is called after layout() is called.

翻译:


  1. updateViewConstraints() 每次layout改变都会调用这个方法。如窗口大小的变化。
  2. viewWillLayout() 会在前一个视图控制器视图的layout() 方法调用前调用,例如,你可以用这个方法来调整约束条件
  3. viewDidLayout()layout()调用方法之后调用。

In all three methods, you must call the super implementation at some point.

翻译:


在这三个方法中,你必须要调用他们的父类实现。

Termination

These are the counterpart methods to creation:

翻译:


这些是和创建方法相对应的方法。

  1. viewWillDisappear() is called before the view disappears. Here you can stop your fancy animations you started in viewDidAppear().
  2. viewDidDisappear() is called after the view is no longer on the screen. Here you can discard everything you no longer need. For example, you could invalidate a timer you used to update your data model on a periodic time base.

翻译:


  1. viewWillDisappear() 会在视图即将消失的时候调用。在这里你可以停止你在viewDidAppear() 中开始的动画
  2. viewDidDisappear () 当视图从屏幕上消失的时候会调用这个方法,在这里你可以丢弃你不再需要的一切。例如,你可以暂停你的定时器去更新你的数据模型。

Life cycle in practice


Now that you know the most important things about a view controller’s life cycle, it’s time for a short test!
Question: Every time OverviewController’s view appears, you want to update the UI to take into account that a user selected a product when the Details tab was selected. Which method would be a good fit?

翻译:


现在你知道了一个视图控制器生命周期中最重要的事情,现在是进行短测试的时候了!
问题:每次 OverviewController’s 的视图出现,当用户选择Details Tab的时候,你想去更新用户的UI。哪个方法是最佳的选择?

Solution Inside
There are two possible methods: viewWillAppear() and viewDidAppear(). The best solution is to use viewWillAppear() so that the user sees the updated UI at the moment the view appears. Using viewDidAppear() means that a user would see the UI appear first showing old data before updating.

翻译:


解决方案
有两种可能的方法:viewwillappear()viewdidappear()。最好的解决办法是使用 viewwillappear() 让用户看到更新的用户界是在视图将要显示的时候。使用 viewdidappear() 意味着用户将要看到第一次显示UI的时候,旧的数据将会被显示在视图上。

你可能感兴趣的:(macOS学习之OS X View Controllers)