UIViewController 视图控制器,继承自UIResponder,作用:管理视图并且响应事件

功能:

1.分担APPdelegate的工作

2.实现模块独立,能提高复用性

创建UIViewController对象:

UIViewController *viewController=[[UIViewController alloc]init];

UIViewController 自身带了一个UiView,默认的大小和屏幕大小一样.

每一个window都带有一个根视图,如果不给根视图赋值,默认根视图就是它本身(window)

self.window.rootViewController=viewController;//设置self.window的根视图

自定义的视图控制器

@implementation MyViewController
//指定初始化方法(当要初始化对象时,一定会走的方法)
//一般不在UIViewController指定初始化方法里做任何操作
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
NSLog(@"%s",__FUNCTION__);
}
return self;
} //加载视图
-(void)loadView {
//如果写了这个方法一定要对UIViewController自带的view初始化
self.view=[[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds] ];
// 重写父类的方法
[super loadView];
// 如果以上操作都不做,loadView和viewDidLoad会走两遍,这时候就出现莫名的错误
NSLog(@"%s",__FUNCTION__);
} //视图加载完成
-(void)viewDidLoad{
// 写了这个必须要对UIViewController自带的 view 初始化
[super viewDidLoad];
// 设置视图控制器view的背景颜色
self.view.backgroundColor=[UIColor grayColor]; UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(50, 20, 200, 40)];
label.backgroundColor=[UIColor yellowColor];
label.text=@"这是标签";
[self.view addSubview:label];
[label release]; UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(50, 60, 200, 40)];
textField.backgroundColor=[UIColor redColor];
textField.placeholder=@"文本输入框";
// 为textfield设置代理
textField.delegate=self; [self.view addSubview:textField];
[textField release]; UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
button.frame=CGRectMake(50, 100, 200, 40);
button.backgroundColor=[UIColor cyanColor];
[button setTitle:@"Button" forState:UIControlStateNormal];
// 给button添加点击事件
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button];
[button release]; NSLog(@"%s",__FUNCTION__);
} //接收内存警告
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning];
NSLog(@"清理内存吧"); // Dispose of any resources that can be recreated.
}
#pragma mark --button点击事件--
-(void)buttonAction:(UIButton *)sender{
self.view.backgroundColor=[UIColor colorWithRed:COLORVALUE green:COLORVALUE blue:COLORVALUE alpha:1.0];
}
#pragma mark --实现代理方法--
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES; }
@end

屏幕旋转

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor greenColor]; UIView *topview=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 200, 100)];
topview.backgroundColor=[UIColor cyanColor];
[self.view addSubview:topview];
topview.tag=100;
[topview release];
}
//支持旋转的方法
//是否允许旋转
-(BOOL)shouldAutorotate{
return YES;
}
//允许的旋转方向
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAll;
}
//屏幕旋转的时候走的方法.iOS8.0会才支持
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
// 拿到屏幕的宽和高
NSStringFromCGSize(size); if (size.width>size.height) {
//横屏
UIView *view=[self.view viewWithTag:100];
view.frame=CGRectMake(0, 50,size.width*2/3, 100);
}else{ UIView *view=[self.view viewWithTag:100];
view.frame=CGRectMake(0, 50,size.width*2/3, 100); } NSLog(@"屏幕旋转了");
}

UIViewController是MVC设计模式的核⼼。

MVC是⼀个框架级的设计模式。

M是Model,主要⽤于建⽴数据模型(即数据的结构)

V是View,我们能看到的所有控件都是view,view主要的功能是展⽰数据。

C是控制器,主要是控制M和V的通信。

版权声明:本文为博主原创文章,未经博主允许不得转载。

UI基础:视图控制器.屏幕旋转.MVC 分类: iOS学习-UI 2015-07-02 22:21 62人阅读 评论(0) 收藏的更多相关文章

  1. Run Loop简介 分类: ios技术 ios相关 2015-03-11 22:21 73人阅读 评论(0) 收藏

    做了一年多的IOS开发,对IOS和Objective-C深层次的了解还十分有限,大多还停留在会用API的级别,这是件挺可悲的事情.想学好一门语言还是需要深层次的了解它,这样才能在使用的时候得心应手,出 ...

  2. NYOJ-32 组合数 AC 分类: NYOJ 2014-01-02 22:21 112人阅读 评论(0) 收藏

    #include<stdio.h> int num[100]; int pnum(int n,int v); int mv=0; int main(){ int n,v; scanf(&q ...

  3. linux常用的压缩与解压缩命令 分类: 学习笔记 linux ubuntu 2015-07-05 19:38 38人阅读 评论(0) 收藏

    1.gzip 压缩 gzip 是压缩文件,压缩之后文件后缀为.gz 用法:gzip 选项 [文件] 2.gunzip 解压 这个命令与gzip的功能刚好相反,这个是解压. 用法 gunzip 选项 [ ...

  4. ubuntu14.04使用root用户登录桌面 分类: 学习笔记 linux ubuntu 2015-07-05 10:30 199人阅读 评论(0) 收藏

    ubuntu安装好之后,默认是不能用root用户登录桌面的,只能使用普通用户或者访客登录.怎样开启root用户登录桌面呢? 先用普通用户登录,然后切换到root用户,然后执行如下命令: vi /usr ...

  5. Raspberry Pi + 3个USB摄像头 + Motion(简易监控设备配置记录1——介绍以及安装) 分类: Raspberry Pi 服务器搭建 2015-04-12 19:21 226人阅读 评论(0) 收藏

    参考: Debian官网链接 Motion官网链接 首先,参见Debian官网链接对Motion的介绍,网页中包含了所有相关依赖包,请首先确保这些依赖包的安装. Motion介绍 摘出对Motion的 ...

  6. shell脚本调试 分类: 学习笔记 linux ubuntu 2015-07-14 12:49 53人阅读 评论(0) 收藏

    1.sh -x script 这将执行脚本并显示所有变量的值 如,脚本: #!/bin/bash #a test about shift if [ $# -le 0 ] then echo " ...

  7. shell入门之流程控制语句 分类: 学习笔记 linux ubuntu 2015-07-10 16:38 89人阅读 评论(0) 收藏

    1.case 脚本: #!/bin/bash #a test about case case $1 in "lenve") echo "input lenve" ...

  8. shell入门之变量测试 分类: 学习笔记 linux ubuntu 2015-07-10 15:49 31人阅读 评论(0) 收藏

    格式:test 测试条件 字符串测试: 注意空格: test str1 == str2 测试字符串是否相等 test str1 != str2 测试字符串是否不相等 test str1 测试字符串是否 ...

  9. shell脚本实现冒泡排序 分类: 学习笔记 linux ubuntu 2015-07-10 14:16 79人阅读 评论(0) 收藏

    手动输入一行字符串,并对其排序. 脚本如下: #!/bin/bash #a test about sort echo "please input a number list" re ...

随机推荐

  1. 使用SFTP工具相关问题

    1.使用SFTP工具,填写ip,端口都正确但是连接不上?         答:请统一使用 filezilla工具进行连接,环境搭建使用该工具进行测试和使用.           2.使用SFTP工具访 ...

  2. 肠道型(enterotype)简介

    An enterotype is a classification of living organisms based on its bacteriological ecosystem in the ...

  3. LeetCode--100--相同的树

    问题描述: 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 / \ / \ 2 3 2 3 [1,2, ...

  4. Jersey 2.x 基于 Servlet 的服务器端应用

    下面的依赖通常应用到应用服务器上(servlet 容器),同时这个应用服务器上没有整合任何 JAX-RS 的实现. 因此,这个应用服务器需要包含有 JAX-RS API 和 Jersey 实现,同时部 ...

  5. Axel and Marston in Bitland CodeForces - 782F (bitset优化)

    题目链接 $dp[0/1][i][x][y]$表示起始边为0/1, 走$2^i$ 步, 是否能从$x$走到$y$ 则有转移方程 $dp[z][i][x][y]\mid=dp[z][i-1][x][k] ...

  6. 『Python』图像金字塔、滑动窗口和非极大值抑制实现

    图像金字塔 1.在从cv2.resize中,传入参数时先列后行的 2.使用了python中的生成器,调用时使用for i in pyramid即可 3.scaleFactor是缩放因子,需要保证缩放后 ...

  7. UVA-1533 Moving Pegs (路径寻找问题)

    Description   Venture MFG Company, Inc. has made a game board. This game board has 15 holes and thes ...

  8. iOS UI-(多)视图控制器的生命周期、加载方法和模态视图方法以及屌丝方法

    #import "ViewController.h" #import "SecondViewController.h" @interface ViewContr ...

  9. ES bulk源码分析——ES 5.0

    对bulk request的处理流程: 1.遍历所有的request,对其做一些加工,主要包括:获取routing(如果mapping里有的话).指定的timestamp(如果没有带timestamp ...

  10. learning uboot how to set ddr parameter in qca4531 cpu

    DDR工作频率  在600MHZ. include/configs/board953x.h #define CFG_PLL_FREQ            CFG_PLL_650_600_200 #d ...