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-)/;
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的更多相关文章
- UI5_HomeWorkCompanyViewController
// // ItemCompany.h // UI5_HomeWork // // Created by zhangxueming on 15/7/3. // Copyright (c) 2015年 ...
- MVC 构造
// // View.h // UI5_HomeWork // // Created by zhangxueming on 15/7/2. // Copyright (c) 2015年 zhangxu ...
随机推荐
- [CoffeeScript] Level 4 Arrays, Objects, Iterations -- Ex
Coffee on the Range Create an array with numbers 1 until 10 using the inclusive (two dot) range synt ...
- Swift用UIBezierPath来画圆角矩形、自定义多路径图形
最好的特点就是可以自定义路径,设置圆角和描边都很方便,以下为代码和效果,均在playground中实现 1.首先实现一个圆角矩形,并对此路径描边,为其绘制一个轮廓. 1 2 3 4 5 6 7 8 9 ...
- java 哈夫曼编码
//哈夫曼树类 public class HaffmanTree { //最大权值 ; int nodeNum ; //叶子结点个数 public HaffmanTree(int n) { this. ...
- Eclipse目录
1. 解决Ubuntu下的Eclipse打开Windows编写的java代码的中文乱码 2. Eclipse常用快捷键
- ASP.NET Core环境配置
一.环境准备 vs2015 update3 下载NET Core Tooling Preview 2 for Visual Studio 2015 (下载地址:https://go.microsoft ...
- nexus-2.13.0-01.war
https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.13.0-01.war
- MeepoPS——轻量级 Socket 服务
MeepoPS是Meepo PHP Socket的缩写.旨在提供高效稳定的由纯PHP开发的多进程SocketService. MeepoPS可以轻松构建在线实时聊天,即时游戏,视频流媒体播放,RPC, ...
- Java最重要的21个技术点和知识点之JAVA集合框架、异常类、IO
(三)Java最重要的21个技术点和知识点之JAVA集合框架.异常类.IO 写这篇文章的目的是想总结一下自己这么多年JAVA培训的一些心得体会,主要是和一些java基础知识点相关的,所以也希望能分享 ...
- C++ atol
函数名: atol 功 能: 把字符串转换成长整型数 用 法: long atol(const char *nptr); 简介编辑 相关函数: atof,atoi,strtod,strtol,st ...
- Python练习题 027:对10个数字进行排序
[Python练习题 027] 对10个数字进行排序 --------------------------------------------- 这题没什么好说的,用 str.split(' ') 获 ...