A.控制器的创建
控制器常见的创建方式有以下几种
通过storyboard创建

直接创建
1 ViewController *vc = [[ViewController alloc] init];
     xib设置了class后,当xib的文件名跟controller类名一样的时候,用这个方法默认就会加载xib中的controller
 
指定xib文件来创建
1 ViewController *vc = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
 
1.从storyboard中创建
(1)创建一个Empty Application (不带storyboard)
 
(2)创建window并加到screen上
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
2 // 手动添加window到screen
3 self.window = [[UIWindow alloc] init];
4 self.window.frame = [[UIScreen mainScreen] bounds];
5 self.window.backgroundColor = [UIColor grayColor];
6 [self.window makeKeyAndVisible];
7 return YES;
8 }
 
(3)创建一个storyboard,拖入一个controller
 
 
 
 
 
(4)取出storyboard(其实就是相当于xib)
1     // 2.取得stroyboard
2 UIStoryboard *sb = [UIStoryboard storyboardWithName:@"mysb" bundle:nil];
 
(5)设置storyboard上controller为rootViewController
 
 
有两种方式取得storyboard上的controller
a.直接使用入口controller,这个view的背景色是橄榄绿
 
设置storyboard中的ViewController的class为自定义的controller
 
1     // 3.1直接使用InitialViewController
2 self.window.rootViewController = [sb instantiateInitialViewController];
 
 
b.使用ID
设置ViewController的class
 
 
再拖入一个ViewController,设置view的背景色是黄色,设置ID是”vc2"
 
1     // 4.使用ID取得controller, 设置rootViewController
2 self.window.rootViewController = [sb instantiateViewControllerWithIdentifier:@"vc2"];
 
 
完成的加载过程:
 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
2 // 1.手动添加window到screen
3 self.window = [[UIWindow alloc] init];
4 self.window.frame = [[UIScreen mainScreen] bounds];
5 self.window.backgroundColor = [UIColor grayColor];
6 [self.window makeKeyAndVisible];
7
8 // 2.取得stroyboard
9 UIStoryboard *sb = [UIStoryboard storyboardWithName:@"mysb" bundle:nil];
10
11 // 3.取出storyboar中的controller
12 // 3.1直接使用InitialViewController
13 ViewController *controller = [sb instantiateInitialViewController];
14
15 // 3.2使用ID
16 ViewController2 *controller2 = [sb instantiateViewControllerWithIdentifier:@"vc2"];
17
18 // 4.设置rootViewController
19 self.window.rootViewController = controller2;
20
21 return YES;
22 }
 
总结:
1.创建Single View Application的时候,项目会自带一个storyboard,其实就是做了上面的事情
设置了Main storyboard 的文件,就会自动加载storyboard
 
2.不同的controller类负责不同的界面的操作
 
2.直接创建
(不详述)
 
3.指定xib文件创建
在之前没有storyboard的时候使用这种方法
(1)创建一个controller
 
(2)创建一个xib
 
(3)在xib拖入两个view,设置一些特征标识,方便对比
 
(4)设置其中一个view为控制器的view
a.更改 File’s Owner 的class为自定义的controller
 
b.设置controller的view
 
(5)从xib加载controller,并把view显示到window上
 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
2 // 1.手动添加window到screen
3 self.window = [[UIWindow alloc] init];
4 self.window.frame = [[UIScreen mainScreen] bounds];
5 self.window.backgroundColor = [UIColor grayColor];
6 [self.window makeKeyAndVisible];
7
8 // 从xib加载控制器, 设置rootViewController
9 self.window.rootViewController = [[XibViewController alloc] initWithNibName:@"myx" bundle:nil];
10
11 return YES;
12 }
 
 
总结:
1.storyboard:(这里使用ViewController2为rootViewController)
 
xib:(使用view1作为显示的view)
 
 
B.创建控制器的view
控制器的view创建有多种方式,(按照优先级进行创建,仅使用最优先的方式)
  • loadView代码(controller实现loadView方法)

  • storyboard描述
  • xib
 
 
 

最新版的官方文档:

 
1.通过loadView
(1)创建一个controller、storyboard、xib
 
(2)配置好storyboard和xib的class为自定义的controller
(3)给storyboard和xib的view加上明显的标志
 
 
(4)在controller类中实现loadView(当controller的view是空的时候,就会调用loadView
 1 // 加载view,这是延迟加载,当需要使用view而view是空的时候调用
2 - (void)loadView {
3 NSLog(@"loadView...");
4 self.view = [[UIView alloc] init];
5 self.view.frame = [[UIScreen mainScreen] bounds];
6 UILabel *label = [[UILabel alloc] init];
7 label.frame = CGRectMake(40, 40, 100, 100);
8 label.text = @"loadView";
9 [self.view addSubview:label];
10 }
 
(5)在delegate中配置controller到window上
a.配置storyboard的controller为rootViewController
 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
2 // 配置window
3 self.window = [[UIWindow alloc] init];
4 self.window.frame = [[UIScreen mainScreen] bounds];
5 self.window.backgroundColor = [UIColor grayColor];
6
7 // 配置storyboard中的controller为rootViewController
8 self.window.rootViewController = [[UIStoryboard storyboardWithName:@"test" bundle:nil] instantiateInitialViewController];
9
10 // 配置xib中的controller为rootViewController,主要要使用带有loadView的controller
11 // self.window.rootViewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
12
13 // 显示window
14 [self.window makeKeyAndVisible];
15 return YES;
16 }
17
会发现没有起作用
 
b.同样,使用xib中的controller为rootViewController,只要loadView存在,也不会起作用
1      // 配置xib中的controller为rootViewController,主要要使用带有loadView的controller
2 self.window.rootViewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
 
总结:
在配置rootViewController的时候,如果配置的controller中实现了loadView方法,就会覆盖storyboard或xib中的view
 
 
 
 

[iOS基础控件 - 6.11.1] - 控制器 & 控制器view的更多相关文章

  1. [iOS基础控件 - 6.11.3] 私人通讯录Demo 控制器的数据传递、存储

    A.需求 1.搭建一个"私人通讯录"Demo 2.模拟登陆界面 账号 密码 记住密码开关 自动登陆开关 登陆按钮 3.退出注销 4.增删改查 5.恢复数据(取消修改)   这个代码 ...

  2. [iOS基础控件 - 6.11.2] - UINavigationController 多控制器 简单使用

    A.概念 1.通常一个app有多个控制器 2.需要对这些控制器进行管理 3.有多个view的时候,用一个父view去管理多个子view 4.控制器管理也是如此,使用给一个父控制器,去控制子控制器   ...

  3. [iOS基础控件 - 6.11.4] storyboard 的 Segue

    A.概念 storyboard中的跳转事件连线,都是一个UIStoryboardSegue对象(Segue)   来源控制器      触发控制器 目标控制器      跳转到的控制器     Seg ...

  4. [iOS基础控件 - 6.11.5] 沙盒 & 数据存储

    A.沙盒 每个APP都有一个沙盒,是独立存在的   1.Xcode5和Xcode6的模拟器文件目录 a.模拟器路径改版 (1)Xcode5中模拟器路径为:/Users/用户名/Library/Appl ...

  5. [iOS基础控件 - 5.5] 代理设计模式 (基于”APP列表"练习)

    A.概述      在"[iOS基础控件 - 4.4] APP列表 进一步封装,初见MVC模式”上进一步改进,给“下载”按钮加上效果.功能      1.按钮点击后,显示为“已下载”,并且不 ...

  6. [iOS基础控件 - 6.12.3] @property属性 strong weak copy

    A.概念 @property 的修饰词   strong: 强指针/强引用(iOS6及之前是retain) weak: 弱智真/弱引用(iOS6及之前是assign)   默认情况所有指针都是强指针 ...

  7. [iOS基础控件 - 6.12.1] QQ菜单管理 UITabBarController 控制器管理

    A.需求 1.类似QQ.微信顶部或者底部的窗口转换导航条 2.给每个页面添加相应内容   B.UITabBarController 1.基本概念: (1)内容高度 iOS7之前内容高度为:屏幕高度 - ...

  8. iOS 基础控件(下)

    上篇介绍了UIButton.UILabel.UIImageView和UITextField,这篇就简短一点介绍UIScrollView和UIAlertView. UIScrollView 顾名思义也知 ...

  9. [iOS基础控件 - 6.10.2] PickerView 自定义row内容 国家选择Demo

    A.需求 1.自定义一个UIView和xib,包含国家名和国旗显示 2.学习row的重用   B.实现步骤 1.准备plist文件和国旗图片     2.创建模型 // // Flag.h // Co ...

随机推荐

  1. iOS学习笔记:iOS核心动画中的常用类型

    CATransaction 当我们在自定义的图层上修改某些支持动画的属性时,系统会为该属性的修改自动产生动画.这种其实属于隐式动画.隐式动画要得益于CATransaction. 一个CATransac ...

  2. 发布 windows 10 universal app 时微软账号验证失败

    具体错误:Visual Studio encountered an unexpected network error and can't contact the Microsoft account s ...

  3. UVa 11235 (RMQ) Frequent values

    范围最值问题,O(nlogn)的预处理,O(1)的查询. 这个题就是先对这些数列进行游程编码,重复的元素只记录下重复的次数. 对于所查询的[L, R]如果它完全覆盖了某些连续的重复片段,那么查询的就是 ...

  4. web交互方式

    轮询:客户端定时向服务器发送Ajax请求,服务器接到请求后马上返回响应信息并关闭连接. 优点:后端程序编写比较容易. 缺点:请求中有大半是无用,浪费带宽和服务器资源. 实例:适于小型应用. 长轮询:客 ...

  5. MongoDB入门分享-笔记整理精选

    最近在学习MongoDB,怕以后忘记,自己做了一个整理,给不知道的小伙伴一起分享学习一下. 第一步> 首先到官网下载,安装MongoDB.(注意MongoDB还有一个可视化管理工具叫: Mong ...

  6. Java [Leetcode 110]Balanced Binary Tree

    题目描述: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced b ...

  7. poj 2762 Going from u to v or from v to u?

    题目描述:为了让他们的儿子变得更勇敢些,Jiajia和Wind将他们带到一个大洞穴中.洞穴中有n个房间,有一些单向的通道连接某些房间.每次,Wind选择两个房间x和y,要求他们的一个儿子从一个房间走到 ...

  8. Cocos2d提供的字体(图文并茂)

    1.AppleGothic CCLabelTTF *myLabel = [CCLabelTTF labelWithString:@"AppleGothic" fontName:@& ...

  9. 域名下Web项目重定向出现DNS域名解析错误问题

    问题: 项目使用的是阿里云的ESC,前几天为IP地址添加了域名 发现发送正常请求时跳转没问题,但发送重定向请求时,页面就会出现DNS域名解析错误的情况 1.我在Tomcat的server.xml中配置 ...

  10. mysql使用经验总结

    在工作中难免会遇到一些这个问题那个问题,当然在mysql中也不例外.今天就让我们来学学mysql中一些比较常用的东西  . 1.有时我们想去查某张表中的字段,但是表中的数据多,字段也很多,如果用sel ...