转自:http://www.cnblogs.com/hxxy2003/archive/2011/10/24/2222838.html

@protocol是Objective-C中的接口定义方式,也就是说在一个类中通过@protocol定义接口,然后在另一个类中去实现这个接口,这也叫“代理”模式, 这种模式在ios开发中经常是会用到的。

“代理”模式的使用:

1.接口声明

#import <Foundation/Foundation.h>

//接口声明
@protocol ProtocolExampleDelegate <NSObject>
@required
-(void)successful:(BOOL)success; @end @interface ProtocolTest : NSObject{
//这个类包含该接口
id<ProtocolExampleDelegate> delegate; }
//接口变量delegate作为类ProtocolTest的属性
@property(nonatomic,retain)id delegate; //定义一个方法,使完成的时候回调接口
-(void)Complete; @end

2.接口回调

#import "ProtocolTest.h"

@implementation ProtocolTest

@synthesize delegate;

-(void)complete
{
//回调接口,successful()由使用者负责具体实现
[[self delegate]successful:YES];
} //通过定时器模拟在任务完成时调用接口回调函数
-(void)start
{
[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(complete) userInfo:nil repeats:YES];
} @end

3.接口实现

#import <UIKit/UIKit.h>
#import "ProtocolTest.h" @class Test; @interface Test : NSObject<UIApplicationDelegate,ProtocolExampleDelegate>
{
UIWindow *window;
ProtocolTest *protocolTest;
} @property(nonatomic,retain)UIWindow *window; @end
#import "Test.h"
#import "ProtocolTest.h" @implementation Test @synthesize window;
//只实现一个方法
-(void)successful:(BOOL)success
{
NSLog(@"completed");
}

这个例子只是理解下@protocol.

Objective-C之@protocol的更多相关文章

  1. SAE/ISO standards for Automotive

    On-Board Diagnostics J1962 Diagnostic Connector Equivalent to ISO/DIS 15031-3: December 14, 2001J201 ...

  2. Automake

    Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...

  3. Objective中的协议(Protocol)

    Objective中的协议(Protocol) 作用: 专门用来声明一大堆方法. (不能声明属性,也不能实现方法,只能用来写方法的声明). 只要某个类遵守了这个协议.就相当于拥有这个协议中的所有的方法 ...

  4. Objective-C( protocol协议)

    protocol 协议 protocol:用来声明方法 1.协议的定义 @protocol 协议名称 <NSObject> // 方法声明列表.... @end 2.如何遵守协议 1> ...

  5. objective c, category 和 protocol 中添加property

    property的本质是实例变量 + getter 和 setter 方法 category和protocol可以添加方法 category 和 protocol中可以添加@property 关键字 ...

  6. objective c, protocol

    OC中协议类似于java中的接口,在多个类具有类似的方法时可以将这些方法定义到protocol中,然后各个类分别实现protocol中的各个方法. 例:有两个类Square和Circle, 定义一个p ...

  7. Objective C Protocol implementation

    protocol 类似于接口,可以实现函数的回调 @protocol MyDelegate<NSObject> -(void)myCallbackFunction; @end //Call ...

  8. The MESI Protocol

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION To provide cache cons ...

  9. CACHE COHERENCE AND THE MESI PROTOCOL

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION In contemporary multi ...

  10. Objective C运行时(runtime)

    #import <objc/runtime.h> void setBeingRemoved(id __self, SEL _cmd) { NSLog(@"------------ ...

随机推荐

  1. nginx安装和配置

    一.安装:yum install nginx service nginx restart/start/check/status/... 二.配置:官网文档 http://nginx.org/en/do ...

  2. [转]使用SSIS创建同步数据库数据任务

    本文转自:http://www.cnblogs.com/heqichang/archive/2012/09/19/2693214.html SSIS(SQL Server Integration Se ...

  3. 让网页在ie浏览器下以最高版本解析网页

    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta ht ...

  4. NDK下vfork+execl启动程序

    pid_t _pid = vfork(); if (_pid == 0) {//child process LOGV("[ContextSharing]in child process.&q ...

  5. vim 命令的使用

    稍微再研究一下vim的命令使用. ----------------------------------------------------------------------------------- ...

  6. (六)SSO之CAS框架扩展 改动CAS源代码实现与ESS动态password验证对接

    题记: 偶尔的偶尔我们会听到这个站点的数据泄露了,那个站点的用户数据泄露了.让用户又一次改动登录password,所以,对于用户数据安全性越发的引起我们的重视了,尤其是一些保密性要求高的站点.更须要添 ...

  7. 数据结构之---C语言实现线索二叉树

    //线索二叉树,这里在二叉树的基础上增加了线索化 //杨鑫 #include <stdio.h> #include <stdlib.h> typedef char ElemTy ...

  8. TortoiseSVN版本管理软件使用简单说明

    很多时候在写一个小的项目不想使用github等工具,只想简单在本地搭建一个版本管理器.那么TortoiseSVN就非常适合. 第一步:下载TortoiseSVN,http://tortoisesvn. ...

  9. java设计模式之策略

    今天你的leader兴致冲冲地找到你,希望你可以帮他一个小忙,他现在急着要去开会.要帮什么忙呢?你很好奇. 他对你说,当前你们项目的数据库中有一张用户信息表,里面存放了很用户的数据,现在需要完成一个选 ...

  10. github 管理图示