//
// 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. 有用好看的CSS+JS+table 导航

    预览效果图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dis ...

  2. HTML之一语言代码

    HTML的lang属性可用于网页或部分网页的语言.这对搜索引擎和浏览器是有帮助的. 同时也可以是指HTTP Header中的Accept-Language/Content-Language. ISO ...

  3. Microsoft Robotics Developer Studio 4

    Microsoft Robotics Developer Studio 4       Share   Language: English   Download Microsoft® Robotics ...

  4. oc-30-堆栈

    /** 操作引用计数器的方式:每个对象内部(对象的堆内存里面)都有一个引用计数器变量,表示对象被引用的次数. 1:retainCount:获得对象的引用计数器的值 2:retain:能够让对象的计数器 ...

  5. open和fopen的区别

    open和fopen的区别: 1.缓冲文件系统缓冲文件系统的特点是:在内存开辟一个“缓冲区”,为程序中的每一个文件使用,当执行读文件的操作时,从磁盘文件将数据先读入内存“缓冲区”, 装满后再从内存“缓 ...

  6. Sublime text 2下alignment插件无效的解决办法

    在sublime text 2中安装了alignment插件,但使用快捷键‘ctrl+alt+a'无效,经过各种方法依然无效,最后找到了这个“Doesn't work at all for me (f ...

  7. V8::Arguments中This和Holder的区别

    ## v8::Arguments namespace v8 { class Arguments { public:  inline int Length() const;  inline Local& ...

  8. C++11实现Qt的信号槽机制

    概述 Qt的信号槽机制是Qt的核心机制,按钮点击的响应.线程间通信等都是通过信号槽来实现的,boost里也有信号槽,但和Qt提供的使用接口很不一样,本文主要是用C++11来实现一个简单的信号槽,该信号 ...

  9. cocos2dx3.0-tinyxml在Android环境下解析xml失败的问题

    本文由@呆代待殆原创,转载请注明出处. 正常情况下,我们在用tinyxml读取xml文件的的时候,会像下面这样写. std::string filePath = FileUtils::getInsta ...

  10. [经典算法] 蒙地卡罗法求 PI

    题目说明: 蒙地卡罗为摩洛哥王国之首都,该国位于法国与义大利国境,以赌博闻名.蒙地卡罗的基本原理为以乱数配合面积公式来进行解题,这种以机率来解题的方式带有赌博的意味,虽然在精确度上有所疑虑,但其解题的 ...