UIViewControllerd的是使用

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值