UI5_HomeWork

//

//  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 init];

    if (self) {

        UIView *bgView = [[UIView alloc] initWithFrame:frame];

        bgView.backgroundColor = [UIColor cyanColor];

        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 yellowColor];

        [btn setTitle:@"NEXT" forState:UIControlStateNormal];

        

        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

        

        [bgView addSubview:btn];

        bgView.tag = 100;

        [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.h

//  UI5_HomeWork

//

//  Created by zhangxueming on 15/7/2.

//  Copyright (c) 2015年 zhangxueming. All rights reserved.

//



#import <UIKit/UIKit.h>



@interface ViewController : UIViewController





@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(0, 20, self.view.frame.size.width, self.view.frame.size.height-100) addTarget:self action:@selector(btnRefreshView)];

    [self.view addSubview:_view];

}



//刷新视图

- (void)btnRefreshView

{

    [_model updateModel];

    [_view updateViewByModel:_model];

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end

 

你可能感兴趣的:(home)