//
// 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. Apache DbUtils - JDBC轻量级封装的工具包

    前段时间使用了Apache Common DbUtils这个工具,在此留个印,以备不时查看.大家都知道现在市面上的数据库访问层的框架很多,当然很多都是包含了OR-Mapping工作步骤的例如大家常用的 ...

  2. Android集成支付宝接口 实现在线支付

    手机的在线支付,被认为是2012年最看好的功能,我个人认为这也是移动互联网较传统互联网将会大放光彩的一个功能. 人人有手机,人人携带手机,花钱买东西,不再需要取钱付现,不再需要回家上网银,想买什么,扫 ...

  3. Java和C++中多态的实现方式

    多态是面向对象的最主要的特性之一,是一种方法的动态绑定,实现运行时的类型决定对象的行为.多态的表现形式是父类指针或引用指向子类对象,在这个指针上调用的方法使用子类的实现版本.多态是IOC.模板模式实现 ...

  4. [AngularJS] Accessing Data in HTML -- controllerAs, using promises

    <!DOCTYPE html> <html> <head> <title>Access Data From HTML</title> < ...

  5. 单片机modebus RTU通信实现,採用C语言,可适用于单片机,VC,安卓等

    当前使用的是STM32+ucos_ii编写的,能够移植到安卓以及VC .NET等方便移植使用,採用modebus poll測试过. 仅仅须要改动响应的通信接口就可以,方便多串口使用 //modebus ...

  6. cocos2d-html5 笔记5: 事件

    在cocos2d里面,通过Node的方式,将整个场景以及里面的object给组织起来,这样很容易画了,从root node开始遍历,把整棵树画出来就是了. 剩下就是animation,timer, 还 ...

  7. c基础补充

    一:字符串在C语言中 字符串一般是动态分配和字符数组两种形式 动态分配的时候字符串字符串指针是4个字节 字符串的大小根据自己的需要来申请 比如: char * c://C为字符串指针 占用四个字节 c ...

  8. tq2440开发板基本配置

    时钟配置及分配 tq2440的晶振频率是12MHz,在uboot中有如下语句:   #define S3C2440_CLKDIV 0x05 /* FCLK:HCLK:PCLK = 1:4:8, UCL ...

  9. Winform下实现图片切换特效的方法

    本文实例讲述了Winform下实现图片切换特效的方法,是应用程序开发中非常实用的一个功能.分享给大家供大家参考之用.具体方法如下: 本实例源自网络,功能较为齐全.丰富!主要功能代码如下: using ...

  10. GCT考试如何准备

    备战考试篇 回首连续的3个月的那段复习过程,感受颇多颇深!以下就各科复习,我谈谈自己的感受和经验: 语文复习: 语文主要是考察你的文学功底和素养以及已经具备的工作生活的常识.从03,04两年的考试真题 ...