//
// 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-, frame.size.height-);
//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-)/;
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 purpleColor];
[btn setTitle:@"NEXT" forState:UIControlStateNormal]; [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; [bgView addSubview:btn];
bgView.tag = ;
self.backgroundColor=[UIColor blackColor];
[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.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)];
// _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 构造的更多相关文章

  1. MVC 构造新Model实现内容搜索

    当前在使用MVC开发一个网站,习惯了使用ASP.NET中控件,转到MVC之后突然对于页面和后台代码的传值感觉无从下手.花了点时间在网上看了写帖子后,想到了一个方法,重新构造一个新Model, 然后利用 ...

  2. ASP.NET Web API实现POST报文的构造与推送

    毕设和OAuth协议相关,而要理解OAuth协议就必须理解HTTP GET/POST方法.因此研究了一下如何使用Web API或MVC构造POST报文并实现客户端与服务器端的交互. 我使用的工具是Vi ...

  3. Web API实现POST报文的构造与推送

    ASP.NET Web API实现POST报文的构造与推送   毕设和OAuth协议相关,而要理解OAuth协议就必须理解HTTP GET/POST方法.因此研究了一下如何使用Web API或MVC构 ...

  4. 从ASP.Net Core Web Api模板中移除MVC Razor依赖项

    前言 :本篇文章,我将会介绍如何在不包括MVC / Razor功能和包的情况下,添加最少的依赖项到ASP.NET Core Web API项目中. 一.MVC   VS WebApi (1)在ASP. ...

  5. web框架django初探

    Web框架介绍 一般会分为两部分:服务器程序和应用程序.服务器程序负责对socket服务器进行封装,并在请求到来时,对请求的各种数据进行整理.应用程序则负责具体的逻辑处理.为了方便应用程序的开发,就出 ...

  6. python学习之——django环境搭建

    Django是一个基于MVC构造的框架,用于web开发,主要目的是简便.快速的开发数据库驱动的网站. 前提:已经安装python 搭建步骤: 1.https://www.djangoproject.c ...

  7. 1.django笔记之django基础

    一.django简介 Django是一个开放源代码的Web应用框架,由Python写成.采用了MVC的软件设计模式,即模型M,视图V和控制器C.它最初是被开发来用于管理劳伦斯出版集团旗下的一些以新闻内 ...

  8. Django练习项目之搭建博客

    背景:自从今年回家过年后,来到公司给我转了试用,我的学习效率感觉不如从前,而且刚步入社会我总是想要怎么想明白想清楚一些事,这通常会花掉,消耗我大量的精力,因为我想把我的生活管理规划好了,而在it技术学 ...

  9. 文成小盆友python-num17 - django基础

    一.首先了解web应用的本质 对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. 下面利用socket实现一个简单的web框架: #!/usr/b ...

随机推荐

  1. Java中this,static,super及finalkeyword和代码块

    this: 能够使用this表示类中的属性------this.name=name 能够使用this强调调用的是本类的方法 能够使用this调用本类的构造方法------this();调用本类中无參构 ...

  2. Effective C++ Item 37 绝不又一次定义继承而来的缺省參数值

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie 经验:绝对不要又一次而来的缺省參数值.由于缺省參数值都是静态绑定,而 virtual 函数 ...

  3. asp.net 后台获取flv视频地址进行播放【转】

    源码下载:http://download.csdn.net/detail/njxiaogui/7609687 前台:.aspx <table> <tr> <td>& ...

  4. NBUT 1225 NEW RDSP MODE I

    找出循环周期即可了 #include<bits/stdc++.h> using namespace std; int N,M,X; int time(int x,int y,int z) ...

  5. 几种 HtmlEncode 的区别(转发)

    问题: HttpUtility.HtmlDecode ,HttpUtility.HtmlEncode  与  Server.HtmlDecode ,Server.HtmlEncode  与 HttpS ...

  6. php常用代码(一)

    一:获取上个小时 方法1:date("H",strtotime("-1 hours"); 方法2:date('H',time()-60*60); 方法3:ech ...

  7. Java再学习——栈(stack)和堆(heap)

    一.内存分配的策略 按照编译原理的观点,程序运行时的内存分配有三种策略,分别是静态的,栈式的,和堆式的. 静态存储分配是指在编译时就能确定每个数据目标在运行时刻的存储空间需求,因而在编译时就可以给他们 ...

  8. Windows 之 CMD命令

    1. ipconfig 查看 Windows IP 配置 2. ping IP地址 查看远程IP是否通畅 2. ipconfig/flushdns 刷新和重置缓存 3. netsh winsock r ...

  9. Java快速入门

    Java 是什么? Java 的特点: 面向对象 平台无关(跨平台): 简单 安全 体系结构 - 中性 可移植 健壮 多线程 解释型 高性能 分布式 动态 Java环境设置: Java SE可免费提供 ...

  10. 读取Excel文件的两种方法

    第一种方法:传统方法,采用OleDB读取EXCEL文件, 优点:写法简单,缺点:服务器必须安有此组件才能用,不推荐使用 private DataSet GetConnect_DataSet2(stri ...