今天模拟Button的delegate来联系一下protocol。

Button类

//  Button.h
// Protocal
//
// Created by WildCat on 13-7-24.
// Copyright (c) 2013年 wildcat. All rights reserved.
// #import <Foundation/Foundation.h>
@class Button;
//定义一个协议
@protocol ButtonDelegate <NSObject>
-(void)onclick:(Button *) button;
@end
//定义一个类 如果要作为Button的代理一定要实现ButtonDelegate
@interface Button : NSObject
@property (nonatomic,retain) id<ButtonDelegate> delegate; -(void)click;
@end
#import "Button.h"

@implementation Button

-(void)dealloc{

    //释放_gelegate
[_delegate release];
[super dealloc]; } -(void)click{ NSLog(@"调用onclick方法");
[_delegate onclick:self];
}
@end

ButtonListener类

/  ButtonListener.h
// Protocal
//
// Created by WildCat on 13-7-24.
// Copyright (c) 2013年 wildcat. All rights reserved.
// #import <Foundation/Foundation.h>
@protocol ButtonDelegate;
@interface ButtonListener : NSObject <ButtonDelegate> @end
#import "ButtonListener.h"
#import "Button.h"
@implementation ButtonListener
-(void)onclick:(Button *)button{ NSLog(@"Button %@ 被点击",button);
}
@end

主函数:

//
// main.m
// Protocal
//
// Created by WildCat on 13-7-24.
// Copyright (c) 2013年 wildcat. All rights reserved.
// #import <Foundation/Foundation.h>
#import "Button.h"
#import "ButtonListener.h"
int main(int argc, const char * argv[])
{ @autoreleasepool {
Button *button=[[[Button alloc] init] autorelease];
Button *button2=[[[Button alloc] init] autorelease]; ButtonListener *listener=[[[ButtonListener alloc] init] autorelease]; button.delegate=listener;
button2.delegate=listener; [button click];
[button2 click]; }
return 0;
}

运行结果:

2013-07-24 16:49:41.344 Protocal[1380:303] 调用onclick方法

2013-07-24 16:49:41.346 Protocal[1380:303] Button <Button: 0x100109990>
被点击

2013-07-24 16:49:41.347 Protocal[1380:303] 调用onclick方法

2013-07-24 16:49:41.347 Protocal[1380:303] Button <Button: 0x1001099e0>
被点击

Object-c学习之路五(@protocol协议)的更多相关文章

  1. Object C学习笔记15-协议(protocol)

    在.NET中有接口的概念,接口主要用于定义规范,定义一个接口关键字使用interface.而在Object C 中@interface是用于定义一个类的,这个和.NET中有点差别.在Object C中 ...

  2. Python学习之路8☞迭代器协议和生成器

    一 什么是迭代器协议 1.迭代器协议是指:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么就引起一个StopIteration异常,以终止迭代 (只能往后走不能往前退) 2.可迭代 ...

  3. redis——学习之路五(简单的C#使用redis)

    redis官方推荐使用的客户端程序 打星星表示推荐使用的客户端程序,一个笑脸表示最近6个月内有过正式活动的.http://redis.io/clients/#c 从这里我们可以判断官方推荐我们使用Se ...

  4. MongoDB学习之路(五)

    MongoDB $type 操作符 类型 数字 备注 Double 1 String 2 Object 3 Array 4 Binary data 5 Undefined 6 已废弃 Object i ...

  5. Java学习之路(五):常见的对象操作

    Object对象 我们先来介绍一下API API(Application Programming Interface):应用程序编程接口 Java API 就是Java提供给我们使用的类,这些类将底层 ...

  6. zigbee学习之路(五):定时器1(查询方式)

    一.前言 今天,我们来学习几乎所有单片机都有的功能,定时器的使用,定时器对单片机来说是相当重要的,有了它,单片机就可以进行一些复杂的工作. 二.原理与分析 谈到定时器的控制,我们最先想到的是要给它赋初 ...

  7. python学习之路 五:函数式编程

    本节重点 掌握函数的作用.语法 掌握作用域.全局变量与局部变量知识 掌握函数名称空间.闭包 一.函数编程基础知识 1.基本定义 函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数 ...

  8. react.js学习之路五

    最近没时间写博客,但是我一直在学习react,我发现react是一个巨大的坑,而且永远填不完的坑 关于字符串的拼接: 在react中,字符串的拼接不允许出现双引号“” ,只能使用单引号' ',例如这样 ...

  9. 嵌入式Linux驱动学习之路(五)u-boot启动流程分析

    这里说的u-boot启动流程,值得是从上电开机执行u-boot,到u-boot,到u-boot加载操作系统的过程.这一过程可以分为两个过程,各个阶段的功能如下. 第一阶段的功能: 硬件设备初始化. 加 ...

随机推荐

  1. java-新浪微博开放平台——话题跟踪

    代码 网盘地址:http://pan.baidu.com/s/1pJ1D0Kz

  2. HDU 3523 Image copy detection(KM最大匹配)

    HDU 3523 Image copy detection 题目链接 题意:这题事实上题意读懂就简单了,说白了就是1-n放到1-n列,每列的值为每列上数字和该数字的差的绝对值,然后求总和最小 思路:就 ...

  3. NEFUOJ 500 二分法+最大流量

    http://acm.nefu.edu.cn/JudgeOnline/problemshow.php?problem_id=500 description 在这个信息化的时代.网购成为了最流行的购物方 ...

  4. easyui 小知识

    默认为今天 $(document).ready(function () {        $(function () {            var curr_time = new Date();  ...

  5. 红米1S Mokee4.4.4 本人编译版耳机线控改动调音量以及上下曲方法

    改动的文件为,用Re管理器编辑: system/usr/keylayout/msm8226-tapan-snd-card_Button_Jack.kl 默认的耳机线控的上下键是切换上下曲功能,因此此文 ...

  6. JS分析URL字符串,取得参数名,AJAX传参请求代码示例

    //当前页面URL中参数分析函数,正则校验 function getQueryString(name) { var reg = new RegExp("(^|&)" + n ...

  7. Mybatis之动态构建SQL语句

    今天一个新同事问我,我知道如何利用XML的方式来构建动态SQL,可是Mybatis是否能够利用注解完成动态SQL的构建呢?!!答案是肯定的,MyBatis 提供了注解,@InsertProvider, ...

  8. 1001 - Another A+B

    1001 - Another A+B Description Give you an integer a, you are to find two another integers which sum ...

  9. 缓存,spring

    applicationcontext.xml xmlns:cache="http://www.springframework.org/schema/cache" xsi:schem ...

  10. 使用pager进行分页

    pager jar网址:http://java2s.com/Code/Jar/t/Downloadtaglibspagejar.htm package com.binary.entity; impor ...