一.View Controller Classes

二.自定义UIVIewController

1.创建

a)nib文件

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
  4. // Override point for customization after application launch.
  5. self.firstViewController = [[[FirstViewController alloc] initWithNibName:@"FirstView" bundle:nil] autorelease];
  6. self.window.rootViewController = self.firstViewController;
  7. self.firstViewController.wantsFullScreenLayout=YES;
  8. [self.window makeKeyAndVisible];
  9. return YES;
  10. }

如果你自定义controller实现initWithCoder:方法的话则会调用,如果没有实现,则调用init.
在调用完后,框架会自动调用controller中的objects(一般是UIView)的awakeFromNib方法,让objects可以有机会来初始化自己.

b)手动创建

只需要alloc&init方式就可以了

2.初始化

一般在controller中实现loadView方法来实现第一次的controller内容的管理,值得注意的是,如果自己实现了这个方法,则不需要调用super的loadView方法

3.设置root view controller的尺寸

a)window的尺寸

b)是否有status bar

c)设备的方向(横向竖向)

d)root view controller的wantsFullScreenLayout属性。这个属性就是设置是否要包含状态栏20像素,比方说你在view里设置了一个button在顶部,这个如果是true,则button出现在状态栏下,如果是false,则会自动顶下去。本身并不影响status bar的隐藏和消失。只是决定status bar下面是否属于程序绘制区域而已。

4.布局

首先可以利用每个view的autoresizingMask的设置来自动布局

viewcontroller布局改变的顺序

a)view controller的vew尺寸改变

b)调用controller的viewWillLayoutSubview.

c)调用view的layoutSubview

d)调用controller的viewDidLayoutSubview.

三.View Controller的生存周期

1.初始化方法:init,initWithCoder;

2.加载controller's view:loadView: controller.view=nil; viewDidLoad: controller.view=view;

3.当收到didREceiveMemoryWarning的时候会调用viewWillUnload,viewDidUnload;

4.dealloc:释放资源(ARC可以忽略)

四.支持界面旋转

1.声明支持的旋转方向

shouldAutorotateToInterfaceOrientation:决定支持的旋转方向(UIInterfaceOrientationIsLandscape的横向2个方向 UIInterfaceOrientationIsPortrait竖直2个方向)

2.如何处理方向改变

a)方向发生改变

b)调用shouldAutorotateToInterfaceOrientation查看支持的方向

c)调用controller的willRotateToInterfaceOrientation:duration方法

d)触发view的自动布局(详细的看第二部分的第4点:布局)

e)调用didRotateFromInterfaceOrientation方法

3.为每个方向创建不同的界面

利用notification来通知不同的状态。

  1. @implementation PortraitViewController
  2. - (void)awakeFromNib
  3. {
  4. isShowingLandscapeView = NO;
  5. [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
  6. [[NSNotificationCenter defaultCenter] addObserver:self
  7. selector:@selector(orientationChanged:)
  8. name:UIDeviceOrientationDidChangeNotification
  9. object:nil];
  10. }
  11. - (void)orientationChanged:(NSNotification *)notification
  12. {
  13. UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
  14. if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
  15. !isShowingLandscapeView)
  16. {
  17. [self performSegueWithIdentifier:@"DisplayAlternateView" sender:self];
  18. isShowingLandscapeView = YES;
  19. }
  20. else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&
  21. isShowingLandscapeView)
  22. {
  23. [self dismissViewControllerAnimated:YES completion:nil];
  24. isShowingLandscapeView = NO;
  25. }
  26. }

4.处理旋转的小贴士

a)暂时禁止任何事件响应

b)存储已经显示的地图区域(地图应用中)

c)如果界面的view层次太过复杂而造成延迟,建议先做view的截图,相关方法我其它博客文章中有提到

d)旋转后,对于tableView的话,需要reload重新读取数据。

e)使用旋转通知来更新应用信息。

五.view显示相关的通知

1.方法有viewWillAppear: viewDidAppear: viewWillDisappear: viewDidAppear:

2.注意这个是controller.view被设置值或者controller.view被设置成nil调用的,当然这个值未必就是直接设置的,可能是通过controller的显示和移除的。

3.知道view显示移除的原因,在上述4个方法中调用isMovingFromParentViewController,isMovingToParentViewController,isBeingPresented,isBeingDismissed 。

六.viewController显示(关闭)其它viewController

在5.0之前,对应的方法是使用model view controller系列的方法。5.0以后增加了presented,prensentingViewController的概念,modalViewController对应5.0后的presentedViewController

  1. FCDemoViewController *controller= [[FCDemoViewController alloc]initWithNibName:@"FCDemoViewController" bundle:nil];
  2. controller.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
  3. [self presentModalViewController:controller animated:YES];
  4. NSLog(@"%@",self.modalViewController);
  5. //5.0
  6. [self presentViewController:controller animated:YES completion:nil];
  7. NSLog(@"%@",self.presentedViewController);
  8. NSLog(@"%@",self.presentingViewController);

属性modalPresentationStyle在iPad下有几种显示形式,对应不同的显示区域。属性modellTransitionStyle决定过渡形式.

关闭也有对应的方法:dismissModalViewControllerAnimated或iOS5.0以后的dismissViewControllerAnimated:completion:

5.0以后其它的变化:

controller可以自定义子controller的集合,这样每一个controller都可以是一个container controller.

definesPresentationContext决定当前显示其它controller的controller是否提供context给presentedViewController(modalViewController),如果不,就会找上一级的controller的该值。

详细的操作可以查看class reference.

苹果官方推荐使用协议的方式来让controller相互通信。首先声明一个协议,并在主controller中实现该协议的方法,在显示其它controller的时候,为其设置delegate=self.这样在其它controller需要回调presentingViewController就可以直接用delegate方法来回调到它。通过这样的方式,可以使得复用性大大增强。而且被显示的controller也不用完全知道显示它的controller的所有信息和属性。

七.controller的edit mode

1.当设置controller的editing属性,会自动触发setEditing:animated属性,这个时候通过myController editButtonItem获得的编辑按钮会自动从edit变成Done,这个通常使用在navigation Controller

比如设置一个右上角按钮为editButton的话代码如下

    1. myViewController.navigationItem.rightBarButtonItem=[myViewController editButtonItem];

UIViewController简述的更多相关文章

  1. iOS开发路线简述

    1 iOS开发环境1.1 开发环境标准的配置是MacOSX+Xcode.MacOSX的话首选用苹果电脑,Macmini最便宜只要4000多就好了然后自己配一个显示器,MacBookPro 也可以,不推 ...

  2. 简述组件化解决方案CTMediator与MGJRouter的主要思想

    简述CTMediator   CTMediator按照功能的结构来讲,使用时需要实现CTMediator的个三部分. 1.CTMediator类:承担总枢纽,总调度的责任 2.Target_(Modu ...

  3. UIViewController生命周期-完整版

    一.UIViewController 的生命周期 下面带 (NSObject)的方法是NSObject提供的方法.其他的都是UIViewController 提供的方法. load   (NSObje ...

  4. 拦截UIViewController的popViewController事件

    实现拦截UIViewController的pop操作有两种方式: 自定义实现返回按钮,即设置UIBarButtonItem来实现自定义的返回操作. 创建UINavigatonController的Ca ...

  5. 简述 OAuth 2.0 的运作流程

    本文将以用户使用 github 登录网站留言为例,简述 OAuth 2.0 的运作流程. 假如我有一个网站,你是我网站上的访客,看了文章想留言表示「朕已阅」,留言时发现有这个网站的帐号才能够留言,此时 ...

  6. JavaScript单线程和浏览器事件循环简述

    JavaScript单线程 在上篇博客<Promise的前世今生和妙用技巧>的开篇中,我们曾简述了JavaScript的单线程机制和浏览器的事件模型.应很多网友的回复,在这篇文章中将继续展 ...

  7. iOS: 在UIViewController 中添加Static UITableView

    如果你直接在 UIViewController 中加入一个 UITableView 并将其 Content 属性设置为 Static Cells,此时 Xcode 会报错: Static table ...

  8. Design Patterns Simplified - Part 3 (Simple Factory)【设计模式简述--第三部分(简单工厂)】

    原文链接:http://www.c-sharpcorner.com/UploadFile/19b1bd/design-patterns-simplified-part3-factory/ Design ...

  9. 8. UIViewController

    1. UIViewController 的认识 UIViewController在iOS开发中占据很重要的位置,iOS的整个UI开发的核心思想也是MVC的架构,从UIViewController的命名 ...

随机推荐

  1. python写入csv文件的几种方法总结

    生成test.csv文件 #coding=utf- import pandas as pd #任意的多组列表 a = [,,] b = [,,] #字典中的key值即为csv中列名 dataframe ...

  2. pandas (loc、iloc、ix)的区别

    loc:通过行标签索引数据 iloc:通过行号索引行数据 ix:通过行标签或行号索引数据(基于loc和iloc的混合) 使用loc.iloc.ix索引第一行数据: loc: iloc: ix:

  3. 在centos下解决 “致命错误:curses.h:没有那个文件或目录”

    当在centos下编译带有头文件<curses.h> 的程序时,出现以下错误: “致命错误:curses.h:没有那个文件或目录” ,最后在“https://zhidao.baidu.co ...

  4. 算法笔记--KMP算法 && EXKMP算法

    1.KMP算法 这个博客写的不错:http://www.cnblogs.com/SYCstudio/p/7194315.html 模板: next数组的求解,那个循环本质就是如果相同前后缀不能加上该位 ...

  5. jsp/servlet区别

    简介: JSP全名为Java Server Pages,中文名叫java服务器页面,其根本是一个简化的Servlet设计,它是由Sun Microsystems公司倡导.许多公司参与一起建立的一种动态 ...

  6. ArcGIS Runtime数据制作教程

    分类: ArcGIS Runtime 2012-04-20 22:25 879人阅读 评论(0) 收藏 举报 工具测试文档file工作c ArcGIS Runtime不能直接加载shp数据,或者mxd ...

  7. Tree Cutting (Hard Version) CodeForces - 1118F2 (树形DP,计数)

    大意:给定树, 每个点有颜色, 一个合法的边集要满足删除这些边后, 每个连通块内颜色仅有一种, 求所有合法边集的个数 $f[x][0/1]$表示子树$x$中是否还有与$x$连通的颜色 对于每种颜色已经 ...

  8. 使用Python生成双色球号码

    说来也是巧,今天和一个朋友聊天,说他运气不错应该买彩票,于是就想到了双色球的规则,就写了几行代码产生双色球号码,代码如下: import random,time def process_int(x): ...

  9. Vim:replace with foobar (y/n/a/q/l/^E/^Y)?

    y:to substitute this match n:to skip this match a:to substitute this and all remaining matches q:to ...

  10. Logger.getLogger()和LogFactory.getLog()的区别

    第一.Logger.getLogger()和LogFactory.getLog()的区别 1.Logger.getLogger()是使用log4j的方式记录日志: 2.LogFactory.getLo ...