UIViewController是iOS程序中的一个重要组成部分,对应MVC设计模式的C,它管理着程序中的众多视图,何时加载视图,视图何时消,界面的旋转等。

1.UIViewController 创建与初始化

  [1].通过nib文件创建与初始化

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
UIViewController *test = [[TestViewController alloc] initWithNibName:@"test" bundle:nil];
self.window.rootViewController = test;
[self.window makeKeyAndVisible]; return YES;
}

  [2].自定义创建与初始化

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
UIViewController *test = [[TestViewController alloc] init];
self.window.rootViewController = test;
[self.window makeKeyAndVisible]; return YES;
}

  

2.UIViewController 旋转方向控制

  • iOS6之前,shouldAutorotateToInterfaceOrientation方法单独控制UIViewController的方向。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
  • iOS6及更高,重写这个2个方法,iOS6里面要旋转还有一些需要注意的地方,需要在Info.plist文件里面添加Supported interface orientations 程序支持的方向.
- (BOOL)shouldAutorotate {
return YES;
} - (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;//竖立
}

3.UIViewController 切换

  • UIViewController自身之间的调用
UIViewController *test = [[UIViewController alloc] init];
//跳转 test
[self.window.rootViewController presentViewController:test animated:YES completion:^{
NSLog(@"present 完成");
}];
//返回到self (test controller消失)
[self.window.rootViewController dismissViewControllerAnimated:YES completion:^{
NSLog(@"dismiss 完成");
}];
  • UINavigationControlle导航控制器的Controller来控制ViewContrller之间的切换(层次逻辑性的ViewContrller之间的切换)
//跳转
[self.navigationController pushViewController:test animated:YES];
//返回
[self.navigationController popViewControllerAnimated:YES];

从以上几点可以看出,UIViewController 之间的切换管理,正确的做法是"谁污染谁治理"。

本站文章为宝宝巴士 SD.Team原创,转载务必在明显处注明:(作者官方网站:宝宝巴士)

转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4771718.html

[Objective-C] 019_UIVIewController的更多相关文章

  1. Automake

    Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...

  2. Objective C中的ARC的修饰符的使用---- 学习笔记九

    #import <Foundation/Foundation.h> @interface Test : NSObject /** * 默认的就是__strong,这里只是做示范,实际使用时 ...

  3. Objective的字符串拼接 似乎没有Swift方便,但也可以制做一些较为方便的写法

    NSString *str1 = @"字符串1"; NSString *str2 = @"字符串2"; //在同样条件下,Objective的字符串拼接 往往只 ...

  4. [转] 从 C 到 Objective C 入门1

    转自: http://blog.liuhongwei.cn/iphone/objective-c/ 进军iPhone开发,最大的难点之一就是怪异的Objective C语法了.不过,了解之后才发现,原 ...

  5. Objective C运行时(runtime)

    #import <objc/runtime.h> void setBeingRemoved(id __self, SEL _cmd) { NSLog(@"------------ ...

  6. Objective C ARC 使用及原理

    手把手教你ARC ,里面介绍了ARC的一些特性, 还有将非ARC工程转换成ARC工程的方法 ARC 苹果官方文档 下面用我自己的话介绍一下ARC,并将看文档过程中的疑问和答案写下来.下面有些是翻译,但 ...

  7. Objective -C学习笔记之字典

    //字典:(关键字 值) // NSArray *array = [NSArray array];//空数组 // NSDictionary *dictionary = [NSDictionary d ...

  8. 刨根问底Objective-C Runtime

    http://chun.tips/blog/2014/11/05/bao-gen-wen-di-objective%5Bnil%5Dc-runtime-(2)%5Bnil%5D-object-and- ...

  9. Objective-C( Foundation框架 一 字符串)

    Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...

  10. Objective C类方法load和initialize的区别

    Objective C类方法load和initialize的区别   过去两个星期里,为了完成一个工作,接触到了NSObject中非常特别的两个类方法(Class Method).它们的特别之处,在于 ...

随机推荐

  1. [CodeForces-259C] Little Elephant and Bits

    C. Little Elephant and Bits time limit per test 2 seconds memory limit per test 256 megabytes input ...

  2. TensorRT入门

    本文转载于:子棐之GPGPU 的 TensorRT系列入门篇 学习一下加深印象 Why TensorRT 训练对于深度学习来说是为了获得一个性能优异的模型,其主要的关注点在与模型的准确度.精度等指标. ...

  3. [bzoj2427]P2515 [HAOI2010]软件安装(树上背包)

    tarjan+树上背包 题目描述 现在我们的手头有 \(N\) 个软件,对于一个软件 \(i\),它要占用 \(W_i\) 的磁盘空间,它的价值为 \(V_i\).我们希望从中选择一些软件安装到一台磁 ...

  4. 线程池(Java中有哪些方法获取多线程)

    线程池(Java中有哪些方法获取多线程) 前言 获取多线程的方法,我们都知道有三种,还有一种是实现Callable接口 实现Runnable接口 实现Callable接口 实例化Thread类 使用线 ...

  5. Spring Cloud学习 之 Spring Cloud Ribbon(负载均衡器源码分析)

    文章目录 AbstractLoadBalancer: BaseLoadBalancer: DynamicServerListLoadBalancer: ServerList: ServerListUp ...

  6. 王颖奇 20171010129《面向对象程序设计(java)》第九周学习总结

    实验九 异常.断言与日志 实验时间 2018-10-25 1.实验目的与要求 (1) 掌握java异常处理技术: (2) 了解断言的用法: (3) 了解日志的用途: (4) 掌握程序基础调试技巧: 2 ...

  7. [hdu5521 Meeting]最短路

    题意:有N个点,给定M个集合,集合Si里面的点两两之间的距离都为Ti,集合里面的所有点数之和<=1e6.有两个人分别在1和N处,求1个点使得两个人到这一点距离的最大值最小 思路:这题是裸的最短路 ...

  8. angular js 分页

    一.编写实体类PageResult public class PageResult implements Serializable { private Long total;//总记录数 privat ...

  9. python--递归函数的学习

    递归:函数间接或者直接调用自己 递归分两个过程 1.往下调用,分解的过程 2.往上回溯,综合的过程 递归的条件: 一定要有结束的条件 例子:阶乘: def fun_a(n): #print(n) if ...

  10. Java中this()和super()

    Java关键字this只能用于方法体中.this只能在类中的非静态方法中使用,静态方法和静态的代码块中绝对不能出现this,并且this只和特定的对象关联,而不和类关联,同一个类的不同对象有不同的th ...