//
// 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-)/;
for (int i=; i<; i++) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, size*i,,size-)];
label.text = [NSString stringWithFormat:@"%d",i+];
label.backgroundColor = [UIColor grayColor];
label.alpha = 0.8;
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor redColor];
[bgView addSubview:label]; UIView *view = [[UIView alloc] initWithFrame:CGRectMake(, size*i, , size-)];
view.tag = +i;
view.backgroundColor = [UIColor blueColor];
[bgView addSubview:view];
} UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(,frame.size.height-,frame.size.width-, );
btn.backgroundColor = [UIColor yellowColor];
[btn setTitle:@"NEXT" forState:UIControlStateNormal]; [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; [bgView addSubview:btn];
bgView.tag = ;
[self addSubview:bgView];
}
return self;
} //根据数据模型修改视图宽度
- (void)updateViewByModel:(DataModel *)model
{
UIView *bgView =(UIView *)[self viewWithTag:];
//NSLog(@"bgView = %@", bgView);
for (int i=; i<; i++) {
UIView *view = [bgView viewWithTag:+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=; i<; i++) {
[_dataArray addObject:[NSNumber numberWithInt:]];
}
}
return self;
} //更新数据模型
- (void)updateModel
{
for (int i=; i<; i++) {
NSNumber *num = [NSNumber numberWithInt:arc4random()%];
[_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(, , self.view.frame.size.width, self.view.frame.size.height-) 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

UI5_HomeWork的更多相关文章

  1. UI5_HomeWorkCompanyViewController

    // // ItemCompany.h // UI5_HomeWork // // Created by zhangxueming on 15/7/3. // Copyright (c) 2015年 ...

  2. MVC 构造

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

随机推荐

  1. SQLSERVER备份事务日志的作用

    事务日志备份有以下3种类型 (1)纯日志备份:仅包含相隔一段时间的事务日志记录,而不包含任何大容量更改 (2)大容量操作日志备份.包括由大容量操作更改的日志和数据页,不支持时间点恢复 (3)尾日志备份 ...

  2. Xcode和IOS模拟器

    Xcode和IOS模拟器 目录 概述 Xcode常用操作 学会用Instrument IOS模拟器 概述 Xcode常用操作 整体缩进或者缩退 command+“[” .command+“]” 在同一 ...

  3. RMI、RPC、SOAP通讯技术介绍及比对 - XML/SOAP

    RMI.RPC.SOAP通信技术介绍及比对 1.RMI 使用java的程序员,对于RMI(RemoteMethod Invoke,远程方法调用)一定不陌生,在java中,为了在分布式应用开发时,能够方 ...

  4. #import与@class的区别

    转自:http://www.cnblogs.com/jqyp/archive/2012/01/13/2321707.html 1.import会包含这个类的所有信息,包括实体变量和方法,而@class ...

  5. 加密数据的填充方式(Padding)

    1.填充数据为填充字节的长度 这种填充方式中,填充字符串由一个字节序列组成,每个字节填充该字节序列的长度.假定块长度为8,原文数据长度9,则填充字节数等于0x07:如果明文数据长度为8的整数倍,则填充 ...

  6. C语言中的各种修饰符

    C允许同时使用多于一个的修饰符,这就使得可以创建一下各种类型: int board[8][8];//int数组的数组 int **ptr;//指向int的指针的指针 int *risk[10];//具 ...

  7. 使用boost的deadline_timer实现一个异步定时器

    概述 最近在工作上需要用到定时器,然后看到boost里面的deadline_timer可以实现一个定时器,所以就直接将其封装成了ATimer类,方便使用,ATimer有以下优点: 可以支持纳秒.毫秒. ...

  8. LeetCode27 Remove Element

    题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...

  9. Code Jam 2008 APAC local onsites Problem C. Millionaire —— 概率DP

    题意: 你有X元钱,进行M轮赌博游戏.每一轮可以将所持的任意一部分钱作为赌注(赌注为0元表示这一轮不押),赌注可以是小数的,不是一定要整数.每一轮 赢的概率为P,赢了赌注翻倍,输了赌注就没了.如果你最 ...

  10. 收藏一部山地车教学视频,Fabien Barel主讲及动作示范

    视频是由曾多次获得UCI速降赛的冠军车手Fabien Barel主讲及动作示范,讲解山地车越野的装备以及基本动作.视频中的要点说明我已经手录为文本,如果视频中没有看清的地方,也可以看文字. 骑行装备 ...