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(@"------------ ...
随机推荐
- Android 开源框架Universal-Image-Loader全然解析(二)--- 图片缓存策略具体解释
转载请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/26810303),请尊重他人的辛勤劳动成果,谢谢! 本篇文章 ...
- 自己动手写了第三阶段的处理器——教学OpenMIPS处理器蓝图
我们会继续上传新书<自己动手写处理器>(未公布).今天是第十条.我每星期试试4 从本章開始将一步一步地实现教学版OpenMIPS处理器.本章给出了教学版OpenMIPS的系统蓝图,首先介绍 ...
- linux+nginx+mysql+php
LNMP(linux+nginx+mysql+php)服务器环境配置 一.简介 Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为 “engine X”, 是一个高性能的 ...
- Android圆弧形ListView的实现
本文带大家来实现ListView的圆弧形的分布排列,原理非常easy,就是依据ListView的每个Item的高度来对每个item进行偏移. 首先自己定义一个LinearLayout,这是ListVi ...
- android插件技术-apkplug于OSGI服务基础-08
我们提供 apkplug 下OSGI使用demo 源代码托管地址为 http://git.oschina.net/plug/OSGIService 一 OSGI与android Service 异同点 ...
- .NET开发必看资料53个+经典源码77个
目录0豆下载:http://down.51cto.com/data/426019 附件预览: 基于.net构架的留言板项目大全源码 http://down.51cto.com/zt/70 ASP.ne ...
- 关于identifier was truncated to '255' characters
学习c++过程中,遇到在VC中使用set时DEBUG模式出现的警告 identifier was truncated to '255' characters in the debug informat ...
- for循环中一个不容小觑的问题
for(int i=1;i<=100;i++) 作为程序猿,我们很喜欢使用这种for循环. 可是,当中隐含着一个重要的问题. 过多的编程经历可能使我们的思维产生了一些误解,在上面的for循环中, ...
- Android于fragment_main.xml文件问题组件收购
package com.dhy.phonedial; import android.app.Activity; import android.app.Fragment; import android. ...
- java IO流文件的读写具体实例
IO流的分类:1.根据流的数据对象来分:高端流:所有的内存中的流都是高端流,比如:InputStreamReader 低端流:所有的外界设备中的流都是低端流,比如InputStream,Output ...