iOS设计模式之二:Delegate模式

来源:互联网 发布:苏联解体后俄罗斯知乎 编辑:程序博客网 时间:2024/05/22 12:42

代理模式 顾名思义就是委托别人去做事情。

iOS中经常会遇到的两种情况:在cocoa框架中的Delegate模式与自定义的委托模式。下面分别举例说明一下:

一、cocoa框架中的delegate模式

在cocoa框架中的Delegate模式中,委托人往往是框架中的对象(视图中的控件、表视图神马的),代理人往往是视图控制器对象。

在我们这个例子中UITableView是委托人,代理人首先得满足一个条件:就是在.h文件中申明它拥有代理资格:

@interface WhateverViewController <<span style="color:#ff0000;">UITableViewDelegate</span>>@end

红色的表示这个视图控制器拥有UITableView的代理资格。

其次,在.m文件中定义委托人可以让代理人去代替做的事情:

//视图控制器来代办应该有多少个节- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {    return [NSArray count];}//视图控制器来代办某个节应该有多少行- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    return [[NSArray objectAtIndex:section]count];}// 视图控制器来代办负责每个栏格的外观- (UITableViewCell *)tableView:(UITableView *)tableView       cellForRowAtIndexPath:(NSIndexPath *)indexPath {     static NSString *CellIdentifier = @"Cell";     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    if (cell == nil) {        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault                 reuseIdentifier:CellIdentifier] autorelease];    }     cell.textField.text = [NSArray objectAtIndex:indexPath.row];      return cell;}//负责当栏格被点击后需要触发的事件- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {    AnotherViewController *anotherViewController = [[AnotherViewController alloc]                                                                         initWithNibName:@"AnotherView" bundle:nil];    [self.navigationController pushViewController:anotherViewController];    [anotherViewController release];}// 这个是可选的,视图控制器勤快我就帮你代办,不勤快我就可以不帮你办这事儿,(提供哪些个行可以被编辑)- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {    return YES;}// 对特定编辑风格进行操作- (void)tableView:(UITableView *)tableView         commitEditingStyle:(UITableViewCellEditingStyle)editingStyle         forRowAtIndexPath:(NSIndexPath *)indexPath {    if (editingStyle == UITableViewCellEditingStyleDelete) {        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];    }    else if (editingStyle == UITableViewCellEditingStyleInsert) {    }}// 可选,对那些被移动栏格作特定操作- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath         toIndexPath:(NSIndexPath *)toIndexPath {}// 对那些可以移动的行返回YES- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {    // 如果不想让栏格移动,就返回NO    return YES;}

好了,当这个委托人需要办这些事时,代理人自己就站出来帮忙办了。这就是ios中的Delegate模式。

二、自定义的delegate模式

@interface A:UIViewid transparendValueDelegate;@property(nomatic, retain) id transparendValueDelegate;@end@implementation A@synthesize transparendValueDelegate-(void)Call{ NSString* value = @"你好";[transparendValueDelegate transparendValue: value];}@end

@interface B:UIViewNSString* value;@end@implementation B-(void)transparendValue:(NSString*)fromValue{value = fromValue;NSLog(@"%@ ,我是B",value); }@end

使用时:
A* a = [[A alloc] init];B* b = [[B alloc] init];a. transparendValueDelegate = b;//设置A代理委托对象为B[a Call];

这样就会输出:

你好,我是B

委托模式关键就在于一个“被”字。这个B是很被动的,随时就会被你A Call一下。

 

三、为什么会有delegate模式

换句话说,它可以用来解决神马问题?

当一个类的某些功能需要被别人来实现,但是既不明确是些什么功能,又不明确谁来实现这些功能的时候,委托模式就可以派上用场。

例如你可以再写个C类,实现

-(void)transparendValue:(NSString*)fromValue {NSLog(@"%@ ,我是C",value); }
也是完全可以的。换谁来,只要它实现了这个方法,我就可以委托它来做这个事。

说到底一切都是为了使类之间的耦合性更松散。好的代码应该对扩展开放,对修改关闭

0 0
原创粉丝点击