zoukankan      html  css  js  c++  java
  • 【iOS开发】协议与委托 (Protocol and Delegate) 实例解析(转)

    1 协议:


    协议,类似于Java或C#语言中的接口,它限制了实现类必须拥有哪些方法。

    它是对对象行为的定义,也是对功能的规范。

    示例:
    1 // GoodChild.h
    2 
    3 #import <Foundation/Foundation.h>
    4 
    5 @protocol GoodChild <NSObject>
    6 
    7 -(void)filialPiety;
    8 
    9 @end
    1 // Student.h
    2 
    3 #import <Foundation/Foundation.h>
    4 #import "GoodChild.h"
    5 //注意实现协议的语法。
    6 @interface Student : NSObject<GoodChild>
    7 
    8 @end
    View Code
     1 // Student.m
     2 // protocol
     3 //
     4 // Created by sxt on 11-10-23.
     5 // Copyright 2011年 __MyCompanyName__. All rights reserved.
     6 //
     7 
     8 #import "Student.h"
     9 
    10 @implementation Student
    11 
    12 - (id)init
    13 {
    14 self = [super init];
    15 if (self) {
    16 // Initialization code here.
    17 }
    18 
    19 return self;
    20 }
    21 -(void)filialPiety{
    22 NSLog(@"孝敬父母。。");
    23 }
    24 @end
    此例中定义了一个协议GoodChild,类Student实现了此协议,所以必须有filialPiety方法。

    每个类虽只有一个父类,但可以实现多个协议,以逗号隔开便可。语法如下:
    1 @interface Student : NSObject<协议1,协议2>
    2 
    3 @end


    2 委托:


    委托是objC中使用非常频繁的一种设计模式,它的实现与协议的使用是分不开的,让我们看一个综合示例:

    小公司老板日常的工作是管理公司、教导新员工、发工资与接电话。

    其中管理公司、教导新员工是老板要亲为的。

    而发工资与接电话老板希望招聘一个秘书来帮忙,于是对秘书的要求就是要略懂出纳发工资,要能帮助领导接电话。 而这两项要求便是协议,对类功能的限定。
     1 // SecProtocol.h
     2 
     3 #import <Foundation/Foundation.h>
     4 
     5 @protocol SecProtocol <NSObject>
     6 //发工资
     7 -(void)payoff;
     8 //接电话
     9 -(void)tel;
    10 
    11 @end
    然后定义一个秘书类
    1 // Sec.h
    2 
    3 #import <Foundation/Foundation.h>
    4 #import "SecProtocol.h"
    5 @interface Sec : NSObject<SecProtocol>
    6 
    7 @end
     1 // Sec.m
     2 
     3 #import "Sec.h"
     4 
     5 @implementation Sec
     6 
     7 - (id)init
     8 {
     9 self = [super init];
    10 if (self) {
    11 // Initialization code here.
    12 }
    13 
    14 return self;
    15 }
    16 
    17 -(void)payoff{
    18 NSLog(@"sec payoff");
    19 }
    20 
    21 -(void)tel{
    22 NSLog(@"sec tel");
    23 }
    24 
    25 @end
    紧接着是老板类:
     1 // Boss.h
     2 
     3 #import <Foundation/Foundation.h>
     4 #import "SecProtocol.h"
     5 @interface Boss : NSObject
     6 
     7 //此属性用于指定秘书对象,此对象必须实现SecProtocol协议。
     8 @property(nonatomic,retain) id<SecProtocol> detegate;
     9 //管理
    10 -(void)manage;
    11 //教导新员工
    12 -(void)teach;
    13 
    14 @end
     1 // Boss.m
     2 #import "Boss.h"
     3 
     4 @implementation Boss
     5 
     6 @synthesize detegate=_detegate;
     7 
     8 - (id)init
     9 {
    10 self = [super init];
    11 if (self) {
    12 // Initialization code here.
    13 }
    14 
    15 return self;
    16 }
    17 
    18 -(void)manage{
    19 NSLog(@"boss manage");
    20 }
    21 
    22 -(void)teach{
    23 NSLog(@"boss teach");
    24 }
    25 
    26 -(void)payoff{
    27 
    28 NSAutoreleasePool *p=[[NSAutoreleasePool alloc] init];
    29 [_detegate payoff];
    30 [p release];
    31 
    32 }
    33 
    34 -(void)tel{
    35 
    36 NSAutoreleasePool *p=[[NSAutoreleasePool alloc] init];
    37 [_detegate tel];
    38 [p release];
    39 
    40 }
    41 @end

     那么老板就具有这4个方法,当调用前2个时是自己完成功能,而调用后2个时则转为调用秘书的方法。

    此时我们跟秘书对象就叫做代理对象,代理模式的名字由来于此。

    最后调用测试下:

     1 // main.m
     2 // delegate
     3 //
     4 // Created by sxt on 11-10-23.
     5 // Copyright 2011年 Jinlong Wei. All rights reserved.
     6 //
     7 
     8 #import <Foundation/Foundation.h>
     9 #import "Boss.h"
    10 #import "Sec.h"
    11 int main (int argc, const char * argv[])
    12 {
    13 
    14 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    15 
    16 //实例化老板对象
    17 Boss *boss=[[[Boss alloc] init] autorelease];
    18 //实例化秘书对象
    19 Sec *sec=[[[Sec alloc] init] autorelease];
    20 
    21 //设置老板的代理对象为秘书
    22 boss.detegate=sec;
    23 
    24 //调用4个方法。
    25 [boss payoff];
    26 [boss tel];
    27 [boss manage];
    28 [boss teach];
    29 [pool drain];
    30 return 0;
    31 }

    转载:dApps开发者 » 【iOS开发】协议与委托 (Protocol and Delegate) 实例解析



     

  • 相关阅读:
    Angular Universal 学习笔记
    SAP Spartacus 如何获得当前渲染页面的 CMS 元数据
    Angular 服务器端渲染的学习笔记(二)
    Angular 服务器端渲染的学习笔记(一)
    第三方外部 Saas提供商如何跟使用 SAP 系统的客户进行对接接口集成
    如何从 SAP Spartacus Product Detail 页面,找到其 Angular 实现 Component 的位置
    具备自动刷新功能的 SAP ABAP ALV 报表
    C++学习目录
    c--条件编译
    c--文件读写--二进制
  • 原文地址:https://www.cnblogs.com/hyzhou/p/2810861.html
Copyright © 2011-2022 走看看