IOS 学习五 ----ViewController

来源:互联网 发布:淘宝众筹是什么意思 编辑:程序博客网 时间:2024/05/16 12:48

之前一直对视图控制器稀里糊涂的,一点都不懂,只知道它是用来控制视图控件的,对控件的动作的响应要放在这里处理。今天就好好看一下这个控制器是干什么用的(刚开始学ios,很多理解估计很不到位,大家不要盲目参考啊!!!)

重点参考:http://blog.csdn.net/ryantang03/article/details/8264072


首先我们看一下官方的资料吧,在这里https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/


UIViewController是UIKit里面的东西,使用它或者继承它的时候我们要import它。

The UIViewController class provides the fundamental view-management model for all iOS apps. 

原来这个UIViewController类为我们提供了基本的视图管理模型。


什么时候我们需要这个控制器呢?

Where necessary, a view controller:

  • resizes and lays out its views

  • adjusts the contents of the views

  • acts on behalf of the views when the user interacts with them

看这个:

测量或者放置view的时候

调整view中的内容的时候

响应用户在view上的动作的时候


A view controller is tightly bound to the views it manages and takes part in the responder chain used to handle events. View controllers descend from theUIResponder class and are inserted into the responder chain between the managed root view and its superview, which typically belongs to a different view controller. If the view controller’s view does not handle an event, the view controller has the option of handling the event or it can pass the event to the superview.

一个视图控制器与视图紧紧地绑定在一起,负责管理这个响应链来处理事物。如果这个视图控制器没有处理事件,那么这个试图控制去可以吧这个事物交给它的父类处理。



View controllers are rarely used in isolation. Instead, you use multiple view controllers, each of which owns a portion of your app’s user interface. For example, one view controller might manage a table of items while a different view controller manages the display of a selected item from that table. Each view controller displays its own views to show the content it is responsible for.

试图控制器都被孤立地使用,我们可以使用多个视图控制器,每个有个控制器都是这个应用程序用户界面的一部分。原来我们可以使用多个试图控制器,一开始我还认为一个app中只能有一个呢。


The UIViewController class provides built-in support for loading a view controller’s views whenever they are needed. Specifically, views are automatically loaded when theview property is accessed.

UIViewController支持内置载入一个另一个控制器的视图。特别地,如果这个view的属性被访问的时候这个视图会自动被加载。


有三种方式来载入这些view

1  使用Storyboard来指定一个view

2 使用这个nib文件

3 重载这个loadView()方法,手工实例化它,然后就可以访问他的属性。



内存的管理

The UIViewController class provides some automatic handling of low-memory conditions through itsdidReceiveMemoryWarning method, which releases unneeded memory.


原来这个方法didReceiveMemoryWarning,是在内存紧张的时候释放不需要的内存滴。


  • addChildViewController:

  • removeFromParentViewController

  • willMoveToParentViewController:

  • didMoveToParentViewController:

这是四个很重要的方法在试图控制器中。


不同状态的相互转换相互转换


image: ../Art/UIViewController Class Reference.pdf

试图控制器的生命周期图:


对于里面的具体方法,参考http://www.cnblogs.com/snake-hand/p/3149631.html                  https://medium.com/@SergiGracia/ios-uiviewcontroller-lifecycle-261e3e2f6133







0 0
原创粉丝点击