您的位置:首页 > 其它

视图控制器的作用

2015-08-18 23:20 211 查看

视图控制器概述

UIViewController:视图控制器。

控制视图显⽰示,响应事件。

分担AppDelegate的⼯工作。

实现模块独⽴立,提⾼高复⽤用性。

视图控制器功能

控制视图⼤大⼩小变换、布局视图、响应事件。

检测以及处理内存警告。

检测以及处理屏幕旋转。

检测视图的切换

自定义视图类

UIViewController⾃自带⼀一个空的view,与需求不符合。

视图控制器只负责控制视图显⽰示,响应事件。

如何设置

⾃自定义视图类继承UIView。在初始化⽅方法中添加⼦子视图控件。

重写controller的loadView⽅方法。创建⾃自定义视图对象,并指定为controller 的view。

将⼦子视图控件对象设置为⾃自定义视图类的属性,在viewDidLoad⽅方法中进 ⾏行设置:添加action、设置delegate等等。

在controller中添加按钮点击事件实现和代理⽅方法的实现。

检测屏幕旋转

视图控制器本⾝身能检测到屏幕的旋转,如果要处理屏幕旋转,需要重写⼏几个

⽅方法:

1. supportedInterfaceOrientations(设置设备⽀支持旋转的⽅方向)

2. willRotateToInterfaceOrientation:duration:(暂停⾳音乐、关闭视图交互等)

3. willAnimateRotationToInterfaceOrientation:duration:(添加⾃自定义动画 等)

4. didRotateFromInterfaceOrientation:(播放⾳音乐、打开视图交互等)。

视图控制

注意视图控制器会⾃自动调整view的⼤大⼩小以适应屏幕旋转,bounds

被修改,触发view的layoutSubviews⽅方法。

view重写layoutSubviews⽅方法,根据设备⽅方向,重新布局。

[UIApplication shareApplication].statusBarOrientation提供设备 当前⽅方向。

容器类视图控制器

常用方法

self.view显⽰示:viewWillAppear: 和 viewDidAppear:

self.view消失:viewWillDisappear: 和 viewDidDisappear:

当self.view添加到⽗父视图上时,执⾏行appear⽅方法: 当self.view从⽗父视 图上移除时,执⾏行disappear⽅方法。

演示执行顺序

定义FirstViewController、SecondViewController类,first作为window 的根视图控制器。

将SecondViewController的view添加到FirstViewController的view上。 将SecondViewController的view移除。 查看SecondViewController中4个⽅方法的执⾏行顺序。

import “LoginViewViewController.h”

import “LoginWiew.h”

@interface LoginViewViewController ()

@property(nonatomic ,retain) LoginWiew *loginView;

@end

@implementation LoginViewViewController

(void)dealloc

{

[_loginView release];

[super dealloc];

}

//如果重写了loadView,系统就不创建自带的view了。

//指定(创建)当前视图控制器直接管理的自定义视图

-(void)loadView

{

self.loginView = [[LoginWiew alloc]initWithFrame:[UIScreen mainScreen].bounds];

self.view = _loginView;
[_loginView release];


}

//创建或初始化视图控制器直接管理的那个自定义视图上得所有子视图

//还会做一些其他的操作:设置代理,给button添加事件,网络请求,数据解析·········

- (void)viewDidLoad {

[super viewDidLoad];

//设置代理
[(LoginWiew *)self.view username].textField.delegate = self;

//给button添加事件
[_loginView.button addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];

self.view.backgroundColor = [UIColor yellowColor];


}

-(void)clickButton

{

NSLog(@”button被点击了”);

}

(BOOL)textFieldShouldReturn:(UITextField *)textField

{

[textField resignFirstResponder];

return YES;

}// called when ‘return’ key pressed. return NO to ignore.

pragma mark —————-检测屏幕旋转

-(NSUInteger)supportedInterfaceOrientations

{

return UIInterfaceOrientationMaskAll;


}

//屏幕将要旋转,(音乐或视频暂停,视图的交互关掉)

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

{

}

//屏幕旋转完成:(音乐或视频取消暂停,视图的交互打开

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

{

}

(void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

给控制器自定视图。


import “LoginWiew.h”

import “LTView.h”

@implementation LoginWiew

/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

// Drawing code

}

*/

(instancetype)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

self.username = [[LTView alloc]initWithFrame:CGRectMake(50, 100, 240, 30) andLabelText:@”用户名” andPlaceHolder:@”电话号码” andDelegate:nil];

[self addSubview:_username];
[_username release];

self.button = [UIButton buttonWithType:UIButtonTypeCustom];
self.button.backgroundColor  = [UIColor blueColor];

[_button setTitle:@"登陆" forState:UIControlStateNormal];

_button.frame = CGRectMake(50, 200, 40, 30);

[self addSubview:_button];


}

return self;

}

//获取当前屏幕的方向

//重新布局

-(void)layoutSubviews

{

//判断当前是左横屏或者是右横屏

if ([UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationLandscapeRight||UIDeviceOrientationLandscapeLeft == [UIApplication sharedApplication].statusBarOrientation) {

self.username.frame = CGRectMake(150, 100, 240, 30);
}else
{
self.username.frame = CGRectMake(50, 100, 240, 30);
}


}

@end

“`
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: