Objective-C该Protocol
Objective-C该Protocol
Protocol
简单来说就是一系列方法的列表,当中声明的方法能够被不论什么类实现,这样的模式一般称为(delegation)模式
在iOS中和OS X中,Apple採用了大量的代理模式来实现MVC中的View(UI控件)和Controller(控制器)
以下举个样例
声明一个Button类和Buttonlisterner类
在Button.h文件中
#import <Foundation/Foundation.h> //类声明
@class Button; //<>代表实现某个协议,NSObject为做根本的协议
//ButtonDelegate为协议的名称
@protocol ButtonDelegate<NSObject> -(void)onClick:(Button*)but; @end @interface Button : NSObject //nonatomic不须要多线程管理,delegate是button的监听器 @property(nonatomic,retain) id<ButtonDelegate> delegate; -(void)Click; @end
在Button.m文件中
#import "Button.h"
@implementation Button
//-(void)dealloc{
// [super dealloc];
//}
-(void)Click{
NSLog(@"Clickbutton被点击了。。。
");
//假设有onClick:的方法则调用此方法
if ([_delegate respondsToSelector:@selector(onClick:)]) {
//而且告诉监听器哪个button被点击了
[_delegate onClick:self];
}
}
@end
在Buttonlisterner.h文件中
#import <Foundation/Foundation.h>
#import "Button.h" //对协议提前声明。跟@class的作用是一样的 @protocol ButtonDelegate; @interface Buttonlisterner : NSObject<ButtonDelegate> @end
在Buttonlisterner.m文件中
#import "Buttonlisterner.h"
#import "Button.h" @implementation Buttonlisterner -(void)onClick:(Button *)but
{
NSLog(@"onClick...");
}
@end
在main.m中
int main(int argc, const char * argv[])
{ @autoreleasepool {
Button* button = [[Button alloc]init];
Buttonlisterner* listenter = [[Buttonlisterner alloc]init];
button.delegate=listenter;
[button Click]; }
return 0;
}
Objective-C该Protocol的更多相关文章
- SAE/ISO standards for Automotive
On-Board Diagnostics J1962 Diagnostic Connector Equivalent to ISO/DIS 15031-3: December 14, 2001J201 ...
- Automake
Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...
- Objective中的协议(Protocol)
Objective中的协议(Protocol) 作用: 专门用来声明一大堆方法. (不能声明属性,也不能实现方法,只能用来写方法的声明). 只要某个类遵守了这个协议.就相当于拥有这个协议中的所有的方法 ...
- Objective-C( protocol协议)
protocol 协议 protocol:用来声明方法 1.协议的定义 @protocol 协议名称 <NSObject> // 方法声明列表.... @end 2.如何遵守协议 1> ...
- objective c, category 和 protocol 中添加property
property的本质是实例变量 + getter 和 setter 方法 category和protocol可以添加方法 category 和 protocol中可以添加@property 关键字 ...
- objective c, protocol
OC中协议类似于java中的接口,在多个类具有类似的方法时可以将这些方法定义到protocol中,然后各个类分别实现protocol中的各个方法. 例:有两个类Square和Circle, 定义一个p ...
- Objective C Protocol implementation
protocol 类似于接口,可以实现函数的回调 @protocol MyDelegate<NSObject> -(void)myCallbackFunction; @end //Call ...
- The MESI Protocol
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION To provide cache cons ...
- CACHE COHERENCE AND THE MESI PROTOCOL
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION In contemporary multi ...
- Objective C运行时(runtime)
#import <objc/runtime.h> void setBeingRemoved(id __self, SEL _cmd) { NSLog(@"------------ ...
随机推荐
- Web Api 2(Cors)Ajax跨域访问
支持Ajax跨域访问ASP.NET Web Api 2(Cors)的简单示例教程演示 随着深入使用ASP.NET Web Api,我们可能会在项目中考虑将前端的业务分得更细.比如前端项目使用Ang ...
- Linux C 编程内存泄露检測工具(二):memwatch
Memwatch简单介绍 在三种检測工具其中,设置最简单的算是memwatch,和dmalloc一样,它能检測未释放的内存.同一段内存被释放多次.位址存取错误及不当使用未分配之内存区域.请往http: ...
- 【译】ASP.NET MVC 5 教程 - 11:Details 和 Delete 方法详解
原文:[译]ASP.NET MVC 5 教程 - 11:Details 和 Delete 方法详解 在教程的这一部分,我们将研究一下自动生成的 Details 和Delete 方法. Details ...
- Java集群--大型网站是怎样解决多用户高并发访问的
时间过得真快,再次登录博客园来写博,才发现距离上次的写博时间已经过去了一个月了,虽然是因为自己找了实习,但这也说明自己对时间的掌控能力还是没那么的强,哈哈,看来还需不断的努力啊!(这里得特别说明一下本 ...
- codeforces 459D - Pashmak and Parmida's problem【离散化+处理+逆序对】
题目:codeforces 459D - Pashmak and Parmida's problem 题意:给出n个数ai.然后定义f(l, r, x) 为ak = x,且l<=k<=r, ...
- J2SE基础:4.面向对象的特性一
面向对象的特性 封装 继承多态 封装: 定义: 通过对象的封装,实现了模块化和信息隐藏. 通过对类的成员施以一定的訪问权限,实现了类中成员 的信息隐藏 注意点: 对象自已该做的一些事情与方法不能交与其 ...
- VS2010关于error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
前段时间自己的系统一直在安装更新.今天突然打开VS2010当运行的时候一直出现error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏这种错误.然后就百度 解决的方法: 1.项目\属 ...
- Windows Phone开发(4):框架和页
原文:Windows Phone开发(4):框架和页 在开如之前,我想更正一个小问题,之前我在第一篇文章中说,Visual Studio Express for Windows Phone的中文版没有 ...
- 远程方法调用(RMI)原理与示例 (转)
RMI介绍 远程方法调用(RMI)顾名思义是一台机器上的程序调用另一台机器上的方法.这样可以大致知道RMI是用来干什么的,但是这种理解还不太确切.RMI是Java支撑分布式系统的基石,例如著名的EJB ...
- sharepoint 2013 使用powershell更改站点集配额和锁定
打开sharepoint powershell 2013,使用管理员方式打开 逐行输入下面命令: $Admin = new-object Microsoft.SharePoint.Administr ...