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

MVC 构造

时间:2015-07-02 22:23:23      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

//
//  View.h
//  UI5_HomeWork
//
//  Created by zhangxueming on 15/7/2.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "DataModel.h"

@interface View : UIView
//构造视图
- (id)initWithFrame:(CGRect)frame addTarget:(id)target action:(SEL)action;

- (void)updateViewByModel:(DataModel *)model;


@end



//
//  View.m
//  UI5_HomeWork
//
//  Created by zhangxueming on 15/7/2.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "View.h"

@implementation View

- (id)initWithFrame:(CGRect)frame addTarget:(id)target action:(SEL)action
{
    self = [super initWithFrame:frame];
    if (self) {
        CGRect frame1=CGRectMake(frame.origin.x, frame.origin.y, frame.size.width-10, frame.size.height-50);
        //CGRect frame1=CGRectMake(frame.origin.x, frame.origin.y, frame.size.width-10, frame.size.height+10);
        UIView *bgView = [[UIView alloc] initWithFrame:frame1];
        bgView.backgroundColor = [UIColor yellowColor];
        CGFloat size = (frame.size.height-80)/12;
        for (int i=0; i<12; i++) {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, size*i,50,size-10)];
            label.text = [NSString stringWithFormat:@"%d",i+1];
            label.backgroundColor = [UIColor grayColor];
            label.alpha = 0.8;
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = [UIColor redColor];
            [bgView addSubview:label];
            
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(50, size*i, 200, size-10)];
            view.tag = 200+i;
            view.backgroundColor = [UIColor blueColor];
            [bgView addSubview:view];
        }
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame = CGRectMake(100,frame.size.height-60,frame.size.width-200, 50);
        btn.backgroundColor = [UIColor purpleColor];
        [btn setTitle:@"NEXT" forState:UIControlStateNormal];
        
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        
        [bgView addSubview:btn];
        bgView.tag = 100;
        self.backgroundColor=[UIColor blackColor];
        [self addSubview:bgView];
    }
    return self;
}

//根据数据模型修改视图宽度
- (void)updateViewByModel:(DataModel *)model
{
    UIView *bgView =(UIView *)[self viewWithTag:100];
    //NSLog(@"bgView = %@", bgView);
    for (int i=0; i<12; i++) {
        UIView *view = [bgView viewWithTag:200+i];
        CGRect frame = view.frame;
        frame.size.width = [model dataFromModelByIndex:i];
        view.frame = frame;
    }
}

@end
//
//  DataModel.h
//  UI5_HomeWork
//
//  Created by zhangxueming on 15/7/2.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface DataModel : NSObject
{
    NSMutableArray *_dataArray;
}

- (id)init;
- (void)updateModel;
- (int)dataFromModelByIndex:(int)index;

@end




//
//  DataModel.m
//  UI5_HomeWork
//
//  Created by zhangxueming on 15/7/2.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "DataModel.h"

@implementation DataModel

- (id)init
{
    self = [super init];
    if (self) {
        _dataArray = [[NSMutableArray alloc] init];
        for (int i=0; i<12; i++) {
            [_dataArray addObject:[NSNumber numberWithInt:0]];
        }
    }
    return self;
}

//更新数据模型
- (void)updateModel
{
    for (int i=0; i<12; i++) {
        NSNumber *num = [NSNumber numberWithInt:arc4random()%300];
        [_dataArray replaceObjectAtIndex:i withObject:num];
    }
    NSLog(@"_dataArray = %@", _dataArray);
}

//获取指定位置视图的宽度

- (int)dataFromModelByIndex:(int)index
{
    return [[_dataArray objectAtIndex:index] intValue];
}


@end
//
//  ViewController.m
//  UI5_HomeWork
//
//  Created by zhangxueming on 15/7/2.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "ViewController.h"
#import "View.h"
#import "DataModel.h"

@interface ViewController ()
{
    DataModel *_model;
    View *_view;
}
@end

//MVC 设计模式
//Model(数据模型)  提供View显示的数据
//View (视图对象)  在View上显示模型数据
//Controller (控制对象)

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self creatModel];
    [self creatUI];
    [self btnRefreshView];
}


- (void)creatModel
{
    _model = [[DataModel alloc] init];
}

- (void)creatUI
{
    _view = [[View alloc] initWithFrame:CGRectMake(10, 40, self.view.frame.size.width, self.view.frame.size.height-100) addTarget:self action:@selector(btnRefreshView)];
   // _view.backgroundColor=[UIColor blueColor];
    [self.view addSubview:_view];
    self.view.backgroundColor=[UIColor redColor];
}

//刷新视图
- (void)btnRefreshView
{
    [_model updateModel];
    [_view updateViewByModel:_model];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

MVC 构造

标签:

原文地址:http://www.cnblogs.com/0515offer/p/4617168.html

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