zoukankan      html  css  js  c++  java
  • IOS设计模式:Delegate模式

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

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

    一、cocoa框架中的delegate模式

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

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

    @interface WhateverViewController < UITableViewDelegate >
    @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:UIView
    id transparendValueDelegate;
    @property(nomatic, retain) id transparendValueDelegate;
    
    @end
    
    @implementation A
    @synthesize transparendValueDelegate
    
    -(void)Call
    { 
    NSString* value = @"你好";
    [transparendValueDelegate transparendValue: value];
    }
    
    @end
    复制代码

     

    复制代码
    @interface B:UIView
    NSString* 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); }也是完全可以的。换谁来,只要它实现了这个方法,我就可以委托它来做这个事。

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

    更多阅读请访问http://www.hopean.com

  • 相关阅读:
    在 Mac OS X 上安装 TensorFlow
    用序列到序列和注意模型实现的:Translation with a Sequence to Sequence Network and Attention
    PyTorch 实战-张量
    PyTorch 实战-用 Numpy 热身
    tf.nn.embedding_lookup TensorFlow embedding_lookup 函数最简单实例
    叩响秋雨梧桐的大门——2018中考之后
    完全背包——01背包方法数
    dp——01背包
    图论最短路——dijkstra
    图论最短路——spfa
  • 原文地址:https://www.cnblogs.com/hopeanCom/p/2774822.html
Copyright © 2011-2022 走看看