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. linux 系统管理的10个小技巧

    1.恢复屏幕 尝试输入:#cat /bin/cat 输入的屏幕内容非常凌乱,那么该怎么做? 输入:#reset 那么屏幕恢复正常了,比关闭再次登录好多了,特别是经过至少5台机器和SSH2才能到达 2. ...

  2. python 判断一个数字是否为4的幂

    def is_Power_of_four(n): while n and not (n & 0b11): n >>= ) print(is_Power_of_four()) pri ...

  3. MVC 子对象数据传递

    1.接受参数 public ActionResult Address(User user) { return View(); } 2. User对象类型 public class User { pub ...

  4. [ios]object-c math.h里的数学计算公式介绍

    参考:http://blog.csdn.net/yuhuangc/article/details/7639117 头文件:<math.h> 1. 三角函数  double sin (dou ...

  5. android中 检查网络连接状态的变化,无网络时跳转到设置界面

    1:在AndroidManifest.xml中加一个声明 <receiver android:name="NetCheckReceiver">    <inten ...

  6. spring事务管理方式大全

    http://blog.csdn.net/baibinboss/article/details/64922472

  7. cookie session localstorage sessionStorage区别

    cookie:http://www.cnblogs.com/Darren_code/archive/2011/11/24/Cookie.html 重要特点: 1.cookie 有大小设置,有过期时间设 ...

  8. Java-Java程序设计的基本概念

    2017-10-06 15:31:39 一.Java程序的基本构成             二.数据类型与标识符 数据类型 标识符 广义的用于定义各种对象名称的字符串集合称为标识符,标识符一般分为用户 ...

  9. Apache配置文件httpd.conf细说

    1.httpd.conf文件位于apache安装目录/conf下2.Listen 88表示监听端口88 此处可以连续写多个端口监听如下: Listen 88 Listen 809 3.目录配置如下: ...

  10. php 邮件发送利器 PHPMailer

    php 自带的邮件发送函数已经弱到不能用了. PHPMailer非常的强大. 绝对是php里必须使用的程序. 下载地址: https://github.com/Synchro/PHPMailer 只要 ...