目标:掌握MVC模式在应用程序是如何工作的,下面通过简单的代码来演示其机制
第一步:创建一个单视图应用程序,其中已经有一个创建好了的控制器,此视图控制器我们用来作为在MVC模式中充当Control的作用(C)
第二步:创建一个协议MyProtocol
#import <Foundation/Foundation.h>
@protocol MyProtocol <NSObject>
- (void) doSomething;
@end
第三步:自定义一个视图MyUIView,此视图继承UIView,并设置一个实现上述协议的代理,复写UIView的触摸结束方法touchesEnded,并让它调用代理方法
.h文件
#import <UIKit/UIKit.h>
#import "MyProtocol.h"
@interface MyUIview : UIView
{
id<MyProtocol>delegate;
}
@property(nonatomic,assign)id<MyProtocol>delegate;
@end
.m文件
#import "MyUIview.h"
@implementation MyUIview
@synthesize delegate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor blueColor];
}
return self;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touchesEnded!");
if (delegate != nil) {
[delegate doSomething];
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
第四步:我门还需要定义我门需要的数据,此时我定义一个类为MyModel,其中定义了一个数组实例变量
.h文件
#import <Foundation/Foundation.h>
@interface MyModel : NSObject
@property(strong,nonatomic)NSMutableArray *colorArray;
@end
.m文件
#import "MyModel.h"
@implementation MyModel
@synthesize colorArray = _colorArray;
- (id)init
{
self = [super init];
if (self) {
self.colorArray = [NSMutableArray array];
[self.colorArray addObject:[UIColor redColor]];
[self.colorArray addObject:[UIColor yellowColor]];
[self.colorArray addObject:[UIColor whiteColor]];
[self.colorArray addObject:[UIColor blueColor]];
[self.colorArray addObject:[UIColor purpleColor]];
[self.colorArray addObject:[UIColor orangeColor]];
[self.colorArray addObject:[UIColor cyanColor]];
}
return self;
}
@end
第五步:我们回到我们的控制器类,此控制器作为MyUIView的代理,他实现了MyProtocol协议,它定义了2个实例变量MyUIView和MyMode,还有一个计数变量count,
在实现文件中做了对协议方法的实现
.h文件
#import <UIKit/UIKit.h>
#import "MyUIview.h"
#import "MyModel.h"
#import "MyProtocol.h"
@interface RootViewController : UIViewController <MyProtocol>
{
MyUIview *myUIView;
MyModel *myModel;
int count;
}
@property(nonatomic,retain)MyUIview *myUIView;
@property(nonatomic,retain)MyModel *myModel;
@end
.m文件
#import "RootViewController.h"
#import "MyUIview.h"
@interface RootViewController ()
@end
@implementation RootViewController
@synthesize myUIView;
@synthesize myModel;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.title = @"RootViewController";
CGRect frame = CGRectMake(0, 320, 320, 140);
myUIView = [[MyUIview alloc] initWithFrame:frame];
myUIView.delegate = self;
myModel = [[MyModel alloc]init];
[self.view addSubview:myUIView];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.myUIView = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)doSomething
{
if (count < [myModel.colorArray count]) {
self.view.backgroundColor = [myModel.colorArray objectAtIndex:count];
count ++;
} else {
self.view.backgroundColor = [myModel.colorArray objectAtIndex:0];
count = 0;
}
}
- (void)dealloc
{
[self.myUIView release];
self.myUIView = nil;
[super dealloc];
}
@end