UIViewController:
UIViewController(视图控制器):控制视图的显示,响应时间,分担AppDelegate的工作,实现模块独立,提高复用性.
其功能主要是:控制视图大小便换,布局变换,响应事件,检测以及处理内存警告,检测以及处理屏幕的选装,检测视图的切换.
1.初始化一个视图控制器
自定义一个UIViewController,例如CyanUIViewController
而后在Appdelegate中初始化:
//视图控制器
//初始化视图控制器
CyanViewController *VC = [[CyanViewController alloc]init];
self.window.rootViewController = VC;
2.UIViewController自带一个空的view,与需求不符(所以需要我们自定义一个视图来替换这个自带的view),视图控制器只负责控制视图的像是和响应事件.
自定义view
//
// CyanView.h
// Uilession3-reviewUIviewController
//
// Created by lanou3g on 15/8/26.
// Copyright (c) 2015年 lanou3g. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface CyanView : UIView
@property(nonatomic,readonly)UILabel *label;
@property(nonatomic,readonly)UITextField *textField;
@end
//
// CyanView.m
// Uilession3-reviewUIviewController
//
// Created by lanou3g on 15/8/26.
// Copyright (c) 2015年 lanou3g. All rights reserved.
//
#import "CyanView.h"
@implementation CyanView
//-(void)dealloc{
// [super dealloc];
//}
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self addAllViews];
}
return self;
}
-(void)addAllViews{
// _label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width / 4, self.frame.size.height)];
// [self addSubview:self.label];
//
// _textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width / 4 * 3, self.frame.size.height)];
// [self addSubview:self.textField];
_label = [[UILabel alloc]initWithFrame:CGRectMake(30, 100, 60, 40)];
_label.backgroundColor = [UIColor redColor];
[self addSubview:_label];
[_label release];
_textField = [[UITextField alloc]initWithFrame:CGRectMake(120, 100, 200, 40)];
_textField.backgroundColor = [UIColor blueColor];
[self addSubview:_textField];
[_textField release];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
而后在CyanUIViewController 中替换其自带的空的view,另我们还可以在CyanViewController.m中实现对我们自定义的视图的控制(修改视图).
//
// CyanViewController.h
// Uilession3-reviewUIviewController
//
// Created by lanou3g on 15/8/26.
// Copyright (c) 2015年 lanou3g. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "CyanView.h"
@interface CyanViewController : UIViewController<UITextFieldDelegate>
@property(nonatomic,retain)CyanView *cyanView;
@end
//
// CyanViewController.m
// Uilession3-reviewUIviewController
//
// Created by lanou3g on 15/8/26.
// Copyright (c) 2015年 lanou3g. All rights reserved.
//
#import "CyanViewController.h"
@interface CyanViewController ()
@end
@implementation CyanViewController
//-(void)dealloc{
// [super dealloc];
//}
//在viewDidLoad方法中,我们最好只是用方法来完成功能,这样会使我们的代码更加条理和清晰.
- (void)viewDidLoad {
[super viewDidLoad];
[self addAllSubviews];
[self addTextforLabel:@"登陆:"];
[self setDelegate];
// Do any additional setup after loading the view.
}
#pragma mark UIView 添加视图
//重写loadView
-(void)loadView{
[super loadView];
}
-(void)addAllSubviews{
self.cyanView = [[CyanView alloc]initWithFrame:self.view.frame];
self.cyanView.backgroundColor = [UIColor cyanColor];
self.view = self.cyanView;
}
#pragma mark UILabel和UITextField 改内容
//添加文本
-(void)addTextforLabel:(NSString *)str{
self.cyanView.label.text = str;
}
#pragma mark 代理和实现代理
-(void)setDelegate{
self.cyanView.textField.delegate = self;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end