码迷,mamicode.com
首页 > 移动开发 > 详细

iOS基础-自定义视图、视图控制器

时间:2015-09-26 14:39:47      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:

自定义视图

例如,登陆页面中,左边label,右边textField。自定义

@interface LTView : UIView

@property (nonatomic,retain) UILabel *label;
@property (nonatomic,retain) UITextField *textField;

- (instancetype)initWithFrame:(CGRect)frame text:(NSString *)text placeHolder:(NSString *)placeHolder;


@end



 @implementation LTView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        //定义宽和高
        CGFloat w = frame.size.width;
        CGFloat h = frame.size.height;
        self.label = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, w/3 - 15, h - 10)];
        [self addSubview:self.label];
        
        self.textField = [[UITextField alloc]initWithFrame:CGRectMake(w/3 - 5, 5, 3*w / 3, h - 10)];
        self.textField.layer.cornerRadius = 8;
        self.textField.layer.borderWidth = 1;
        [self addSubview:self.textField];
        
        [self.label release];
        [self.textField release];
        
    }return self;
}

- (void)dealloc{
    [self.label release];
    [self.textField release];
    
    [super dealloc];
}



- (instancetype)initWithFrame:(CGRect)frame text:(NSString *)text placeHolder:(NSString *)placeHolder{
    self = [self initWithFrame:frame];
    if (self) {
        self.label.text = text;
        self.textField.placeholder = placeHolder;
    }
    return self;
    
}

 

 

视图控制器

概述UIViewController:视图控制器

控制视图显示,响应时间

分担AppDelegate的工作

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

 

功能

控制视图大小变化,布局视图,响应事件

检测以及处理内存警告

检测以及处理屏幕旋转

检测视图的切换

 

MVC概述

UIViewController是MVC设计模式的核心

MVC是一个框架级的设计模式

M是Model,主要用于建立数据模型

V是View,View主要功能是展示数据

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

 

UIViewController自带一个空的View,与需求不符合,

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

所以使用自定义视图类:

1.自定义视图继承UIView,在初始化方法中添加子视图控件

2.重写controller的loadView方法,创建自定义试图对象,并指定为controller的View

3.将子视图控件对象设置为自定义视图类的属性,在viewDidLoad方法中进行设置

 

@implementation ViewController


-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        NSLog(@"1 ");
    }return self;
}

- (void)loadView{
    [super loadView];
    //重写
    UIView *view = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    view.backgroundColor = [UIColor brownColor];
    self.view = view;
    NSLog(@"2");
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    NSLog(@"3");
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:YES];
    NSLog(@"4");
}

 

 

 

//视图控制器功能一、
//1.检测并响应时间
//2.视图的整体布局
//3.检测屏幕旋转
//4.检测并处理内存警告

//1.屏幕能否旋转(默认旋转)
- (BOOL)shouldAutorotate{
    return YES;
}
//屏幕能旋转的方向
- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

 

 

//内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    //如果视图被加载,而且视图没有显示,将其release
    if ([self isViewLoaded] == YES && self.view.window == nil) {
        self.view = nil;
    }
    // Dispose of any resources that can be recreated.
}

 

iOS基础-自定义视图、视图控制器

标签:

原文地址:http://www.cnblogs.com/dingjianjaja/p/4840735.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!