UIViewController学习笔记

来源:互联网 发布:做微商必备哪些软件 编辑:程序博客网 时间:2024/04/29 13:22

UIViewController : UIResponder:NSObject

A view controller manages a set of views that make up a portion of your app’s user interface. It is responsible for loading and disposing of those views, for managing interactions with those views, and for coordinating responses with any appropriate data objects. View controllers also coordinate their efforts with other controller objects—including other view controllers—and help manage your app’s overall interface.

UIViewController是用来加载与处理视图,管理视图间的交互,协调对基本数据对象的响应.管理视图的层次结构。

重要属性

12th,Dec,2016

automaticallyAdjustsScrollViewInsets

应对scrollView的全屏布局的,会依据viewController所处的环境(是否有navigationBar或者tabBar之类的bar), 在UIViewController的view moveToWindow的时候,自动设置scrollView的 contentInset 和 scrollIndicatorInsets来保证内容不被挡住, 但完美使用且不出问题要满足以下的条件:
1.UIScrollView是 UIViewConroller的view的第一个subview.
2.UIScrollView的位置正好是view的bounds


21th,Sep,2016

简介

ViewControllers的两种类型

content view controller

管理独立的应用界面

manage a discrete piece of your app’s content and are the main type of view controller that you create.

每个view Controller都有一个root view,root view下可以有多个子view.
列表内容
图片 from Apple

container view controller:

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.
A container view controller manages its own views plus the root views from one or more of its child view controllers.
It manages only the root view, sizing and placing it according to the container’s design.
hild view controller together with a navigation bar and optional toolbar

如图展示了container view controller与child view controller之间的关系,containerViewController管理childViewController的整体尺寸与位置,childViewController管理视图的内容
这里写图片描述
图片 from Apple

职责

A view controller’s main responsibilities include the following:
1. Updating the contents of the views, usually in response to changes to the underlying data.
2. Responding to user interactions with views.
3. Resizing views and managing the layout of the overall interface.

ViewController主要用于:
1. 对基础数据的改变进行响应,更新视图;
2. 响应用户交互;
3. 调整视图的尺寸与管理整体布局


23th,March,2017

加载视图控制器

1) StoryBoard加载:

[[UIStoryboard storyboardWithName:@"storyboard的名称,比如Main" bundle:nil] instantiateViewControllerWithIdentifier:@"在storyboard中设置视图控制器的StoryboardID"];

2) Nib加载:

DemoViewController *vc = [[DemoViewController alloc] initWithNibName: @"" bundle: nil];

3) 代码写UI:

通过在UIViewController的实现文件中实现loadView方法,创建视图层次,可以将根视图赋值给view属性。

- (void)loadView {   self.view = [[CustomeView alloc] init];}

生命周期

1.Xib/SB 加载

生命周期: initWithCoder或者intWithNibName:Bundle或init从归档文件(xib/SB)加载UIViewController对象 —> 连接outlet和action —> 调用loadView方法—> 调用视图的initWithCoder与awakFromNib—>viewDidLoad—>视图加载完毕—> viewWillAppear—>更新布局—> 视图显示在屏幕上—> viewDidAppear

2.代码加载

生命周期: init -> loadView —> viewDidLoad —> viewWillAppear —> viewDidAppear —> viewWillDisappear —> viewDidDisappear —> viewWillUnload —>销毁视图 —> viewDidUnload —> dealloc

loadView

调用时间:访问ViewController的view属性且该属性为nil时会调用loadView。不可手动调用。比如初始化的时候或者内存紧张在viewDidUnload中将view设为nil,再次访问该页面的view时则会通过该方法重建view。

作用:负责创建UIViewController的view属性

默认实现:即super方法的实现做了哪些工作: 会先查找UIViewController相关联的文件,通过加载xib文件创建view属性(ps: 加载时指定了xib文件,则从相应xib加载,没有则从与类名相同的xib文件加载, 如果没有找到xib文件则创建空白view—> 以前是创建空白view,现在如果没有设置view则会显示黑色view;还有通过init初始化视图,且存在同名xib文件时,如果没有实现loadView则会从xib加载,但实现了loadView,不论是否执行父类方法也无法显示xib内容,移除了loadView方法就可以正常显示xib内容)。

实践: 代码写UI时,则需重写loadView方法,且不要调用super方法。当然调用了也不会报错,只是造成了一些不必要的开销。

viewDidLoad

调用时间:view创建完毕后

作用:初始化工作,界面元素的初始化比如添加或者移除一些子视图,从数据库或网络加载数据,设置视图的初始属性,修改一些约束

viewWillAppear

调用时间:view被添加到superview之前,切换动画之前调用

作用:做一些显示前的处理,如键盘弹出,特殊的过程动画(比如状态调,导航栏的颜色),做一些比较耗时的操作

viewWillLayoutSubviews

作用: 通知即将开始对视图进行布局

viewDidLayoutSubviews

作用: 通知视图的布局已经完成

viewDidAppear

调用时间:切换动画之后调用

作用:切换动画后需要执行的操作。

viewDidUnload

调用时间: 内存紧张时,系统会发出内存警告,UIViewController会受到didReceiveMemoryWarning消息,该消息默认实现会将不在视图层次结构中的view,即view的superview为nil的时候,将view释放,然后调用viewDidUnload方法.

作用: 释放资源,释放界面元素相关的资源。

Tip

1.UIViewController的view是通过懒加载的,只有调用view属性的getter方法才会创建view。

2.init中不要创建view。 而应该只做相关数据的初始化,且是关键数据,并且也不要调用view属性。

3.init方法如果重载了loadView方法,则不会从xib/SB 中载入视图。调用initWithNib重载loadView只是继承super方法,且不对view属性做set操作则会从xib/SB中载入视图。

参考资料

Apple Reference-UIViewController-UIKit
View Controller Programming Guide for iOS.

0 0
原创粉丝点击