例如,我们要在一个 ViewController 中使用一个ActionSheet,代码如下:

  UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"Delegate Example"delegate:self // telling this class to implement UIActionSheetDelegatecancelButtonTitle:@"Cancel"destructiveButtonTitle:@"Destructive Button"otherButtonTitles:@"Other Button",nil[actionSheet showInView:self.view];

中间的代码: delegate:self 来告诉当前这个ViewController 来实现 UIActionSheetDelegate
这样做的原因是
ViewController 和 ActionSheet 二者是相互独立的,但是当用户点击了 ActionSheet 上的按钮时,ViewController 需要处理 ActionSheet 上按钮的点击事件,这样做相当于通知 ViewController 监听 UIActionSheetDelegate,以便我们处理 ActionSheet 上按钮的点击事件。

注意:
delegte:self; 这行代码仅是告诉 ViewController 实现 UIActionSheetDelegate,但是我们还需要告诉类实现 UIActionSheetDelegate protocol (协议),方法是在 .h 文件中 加入,如下所示:

@interface DelegateExampleViewController : UIViewController <UIActionSheetDelegate>
  • 我们自己创建的类 (CustomClass),如何使用 delegate

在CustomClass.h 文件中 首先要为这个 delegate 定义 protocol (写在 @interface 之前)
在 protocol 和 end 之间定义 protocol 方法, 这个方法可以被任何使用这个代理的类所使用

#import
@class CustomClass;//为Delegate 定义 protocol
@protocol CustomClassDelegate
//定义 protocol 方法
-(void)sayHello:(CustomClass *)customClass;
@end@interface CustomClass : NSObject {}// define delegate property
@property (nonatomic, assign) id  delegate;// define public functions
-(void)helloDelegate;@end

.m 文件中没什么特别的,最重要的要实现 helloDelegate 方法

-(void)helloDelegate {// send message the message to the delegate![delegate sayHello:self];
}

接下来我们切换到要使用这个类的 ViewController ,实现上面刚刚创建的 delegate

// DelegateExampleViewController.h
// import our custom class#import "CustomClass.h"@interface DelegateExampleViewController : UIViewController <CustomClassDelegate> {}
@end

在 DelegateExampleViewController.m 文件中,我们需要初始化这个 custom Class,然后让 delegate 给我们传递一条消息

//DelegateExampleViewController.mCustomClass *custom = [[CustomClass alloc] init];// assign delegate
custom.delegate = self;
[custom helloDelegate];

还是在 DelegateExampleViewController.m 我们要实现在 custom Class.h 中生命的 delegate function

-(void)sayHello:(CustomClass *)customClass {NSLog(@"Hi!");
}

Bingo!

如何理解 Objective-C Delegate相关推荐

  1. IOS学习笔记——Objective-c基础(一)

    2019独角兽企业重金招聘Python工程师标准>>> 最近自学ios,ios的支持语言是Objective-c,所以要学习ios就需要先学习Objective-c语言.当掌握了Ob ...

  2. .NET Framework源码研究系列之---Delegate

    前言 曾几何时能看到微软产品的源码简直是天方夜谭,不过现在这却成了现实,微软终于对外开放了它的产品的源代码.抛去开源运动与微软之间的世代情仇,抛去微软这一做法的初衷,这总归是件好事,能够让我们拨开云雾 ...

  3. 理解Kotlin语言独有的位置注解,让注解控制更精准

    在Kotlin语言编写的代码中,你应该看到过类似这样的注解@file:JvmName(...),这有点难以理解,正常的注解不会存在类似@file:这样的前缀,在Java语言中也没有类似的语法.那么,这 ...

  4. 转:Object-Runtime的基本数据类型

    Class Objective-C是支持反射的,先来了解一下其如何表达一个类.在Objective-C的Runtime中有个类型是Class(只在Runtime环境中使用),用来表示Objective ...

  5. C#语法:委托与方法

    1.了解委托(delegate) * 委托是一种全新的面向对象语言特性,运行于.NET平台 *基于委托开发事件驱动程序变得简单 *使用委托可以大大简化多线程编程的难度 2.理解委托 *委托(deleg ...

  6. Objective-C Runtime的数据类型

    转自:http://www.cnblogs.com/whyandinside/archive/2013/02/26/2933552.html Class Objective-C是支持反射的,先来了解一 ...

  7. 委托与事件-闲聊系列(二)

    系列一讲述了生活中的委托与事件的一些场景,本篇我们从C#语言的角度来阐述委托的含义.我们设想一个如下场景:中午下课铃声响了,你去学校食堂排队打饭,你女朋友不想走路,让你打包一份饭给她.下面直接上代码: ...

  8. 【简单3d网络游戏制作】——基于Unity

    目录 Demo展示 前期知识点准备 1.delegate委托 2.通信协议 3.List容器 4.dictionary容器 5.MethodInfo类 进入创作 c/s通用通信协议: 客户端 1.场景 ...

  9. iOS 知识点整理 (持续更新...)

    整理了些iOS相关的基础问题,每个问题可能会再写些扩展,需要具体了解可以看题目下方的链接 如有错漏,欢迎指出,谢谢 一.Swift 1.给一个数组,要求写一个函数,交换数组中的两个元素(swift可用 ...

  10. Objective C内存管理之理解autorelease------面试题

    Objective C内存管理之理解autorelease Autorelease实际上只是把对release的调用延迟了,对于每一个Autorelease,系统只是把该Object放入了当前的Aut ...

最新文章

  1. 【译】function.caller 被认为是有害的
  2. mysql主从复制篇-主库有数据
  3. git commit Please tell me who you are it config --global user.email you@example.com
  4. springboot链接MySQL线程池_SpringBoot中的数据库连接池
  5. CTF-web文件上传漏洞
  6. 12个ggplot2扩展包帮你实现更强大的可视化
  7. 对I2C总线协议的一些理解
  8. Mybatis自定义分布式二级缓存实现与遇到的一些问题解决方案!
  9. SQL中COUNT()函数介绍
  10. python读取文件大小、时间_Python从netCDF文件读取数据,时间为测量开始后的“秒数”...
  11. iOS边练边学--(Quartz2D)图片裁剪,带圆环的裁剪
  12. mysql执行计划(explain)
  13. visio破解版安装2013
  14. java web 实战开发经典_java web 开发实战经典(一)
  15. 基于JavaWeb实现网上图书商城系统
  16. 服务器扩容申请文档,服务器内存扩容的申请
  17. 手撕Boost!Boost公式推导及实验验证
  18. linux安装Ice3.7 c++
  19. 了解公司企业:Primetics
  20. 安卓 获取屏幕坐标(点击屏幕获取坐标)

热门文章

  1. UFLDL教程: Exercise: Implement deep networks for digit classification
  2. 机器学习之 weka学习(四)
  3. UWP_小说在线阅读器:功能要求与技术要求
  4. 利用api接口来实现web网页登陆
  5. Delphi编译报错对照表
  6. Http请求报头设置(C#)
  7. AutoFac自动注入时报错
  8. LintCode 6.合并排序数组 ||
  9. 术语-服务:PaaS
  10. 期货黄金与现货黄金比较