4 9.OC15-Protocol
Protocol
1)、简单来说就是一系列方法的列表,其中声明的方法可以被任何类实现。这种模式一般称为代理(delegation)模式。
2)、在IOS和OS X开发中,Apple采用了大量的代理模式来实现MVC中view(UI控件)和Controller(控制器)的解耦。
Main.m
// // main.m // OC10-内存管理2-set方法的内存管理 // // Created by qwz on 13-12-9. // Copyright (c) 2013年 renhe. All rights reserved. //
#import <Foundation/Foundation.h> #import "Button.h" #import "ButtonListener.h" #import "MyListener.h"
int main(int argc, const char * argv[]) {
@autoreleasepool { //初始化一个按钮 Button *button = [[[Button alloc] init] autorelease]; //初始化一个按钮的监听器 //ButtonListener *listener = [[[ButtonListener alloc] init] autorelease]; MyListener *listener = [[[MyListener alloc] init] autorelease];
//设置按钮的监听器 button.delegate = listener;
//点击按钮 [button click];
Button *button2 = [[[Button alloc] init] autorelease]; button2.delegate = listener; //点击button2 [button2 click]; } return 0; }
|
Button.h
// // Button.h // OC10-内存管理2-set方法的内存管理 // // Created by qwz on 13-12-10. // Copyright (c) 2013年 renhe. All rights reserved. //
#import <Foundation/Foundation.h> @classButton;
// <>代表实现某个协议 @protocol ButtonDelegate <NSObject> - (void)onClick:(Button *)btn; @end
@interface Button : NSObject
// delegate 就是按钮的监听器 @property (nonatomic, retain) id<ButtonDelegate> delegate;
// 点击按钮 - (void)click;
@end
|
Button.m
// // Button.m // OC10-内存管理2-set方法的内存管理 // // Created by qwz on 13-12-10. // Copyright (c) 2013年 renhe. All rights reserved. //
#import "Button.h"
@implementation Button
- (void)dealloc{ [_delegaterelease]; [super dealloc]; }
- (void) click{ //如果delegate实现了onClick:这个方法 if( [_delegate respondsToSelector:@selector(onClick:)]){ //按钮被点击了,就应该通知监听器,并且告诉监听器哪个按钮被点击了 [_delegate onClick:self]; }else{ NSLog(@"监听器并没有实现onClick:方法"); } }
@end |
ButtonListener.h
// // ButtonListener.h // OC10-内存管理2-set方法的内存管理 // // Created by liuyes on 13-12-10. // Copyright (c) 2013年 renhe. All rights reserved. //
#import <Foundation/Foundation.h>
//#import "Button.h" //对协议进行提前声明,跟@class的用途是一致 @protocolButtonDelegate;
@interface ButtonListener : NSObject<ButtonDelegate>
@end |
ButtonListener.m
// // ButtonListener.m // OC10-内存管理2-set方法的内存管理 // // Created by liuyes on 13-12-10. // Copyright (c) 2013年 renhe. All rights reserved. // #import "ButtonListener.h" #import "Button.h"
@implementation ButtonListener
- (void)onClick{ NSLog(@"ButtonListener已经监听到按钮被点击了"); }
@end |
MyListener.h
// // MyListener.h // OC10-内存管理2-set方法的内存管理 // // Created by liuyes on 13-12-10. // Copyright (c) 2013年 renhe. All rights reserved. //
#import <Foundation/Foundation.h> //#import "Button.h" @protocolButtonDelegate;
@interface MyListener : NSObject <ButtonDelegate>
@end |
MyListener.m
// // MyListener.m // OC10-内存管理2-set方法的内存管理 // // Created by liuyes on 13-12-10. // Copyright (c) 2013年 renhe. All rights reserved. //
#import "MyListener.h" #import "Button.h"
@implementation MyListener
- (void)onClick{ NSLog(@"MyListener已经监听到按钮被点击了"); }
- (void)onClick:(Button *)btn{ NSLog(@"MyListener已经监听到按钮-%@被点击了", btn); }
@end |
相关推荐
在OC中, 延展的使用在以后IOS开发过程中是很普遍很常用的, 在这里简单的介绍了延展的使用方法以及步骤 ,希望对你有帮助.
- -25oC ~ 85oC ● Packcage - 153-ball FBGA - 11.5x13mm2, 1.0t, 0.5mm pitch - Lead & Halogen Free [ e-NAND Flash ] ● Packaged NAND flash memory with MultiMediaCard interface ● High capacity memory ...
让oc像swift那样给@protocol协议添加方法的默认实现教程加demo,例子里面实现了xib文件,遵守协议以后直接就能被调用的例子
managers who want to understand the security features of OC4J. It discusses the Oracle Application Server Java Authentication and Authorization Service (JAAS) Provider in detail, as well as discussing...
东西不多简单易懂、Swift语言如何使用UITableView、Swift与OC的混合开发、扩展(Extension)的简单使用、协议(protocol)
操作系统:linux软件环境:Python 2.7.3 用法:复制代码 代码如下:$ ./MonSocket.py # This is check the URI or Socket of ... This is Socket protocol ./MonSocket.py -p port; This is Socket port ./MonSocket.
Line Protocol 直译”行协议“,InfluxDB的行协议是一种写入数据点到InfluxDB的文本格式。必须要是这样的格式的数据点才能被InfluxDB解析和写入成功。 weather,location=us-midwest temperature=82 ...
rsync error: error starting client-server protocol (code 5) at main.c(1516) [Receiver=3.0.9] rsync服务器配置如下 [root@localhost ~]# cat /etc/rsyncd.conf # /etc/rsyncd: configuration
详解 objective-c中interface与protocol的作用 以前对Objective-C中的interface,即头文件的作用一直不太清楚。最近看了一些文章,再加上自己的试验,对头文件的作用稍有了解。 在我看来,头文件的作用是,定义对外...
使用@EnableDubbo启用dubbo, 配置dubbo.protocol.xxx相关属性,不设置dubbo.protocol.id,启动应用将会抛出异常. java.lang.IllegalStateException: Invalid name=“com.alibaba.dubbo.config.ProtocolConfig#0” ...
面向对象语法 OC特有语法 内存管理 Category Protocol block ARC
– 4-to-26 MHz crystal oscillator – Internal 16 MHz factory-trimmed RC – 32 kHz oscillator for RTC with calibration – Internal 32 kHz RC with calibration • Power consumption – Run: 100 µA/MHz ...
Protocol Buffers (类似XML的一种数据描述语言)最新版本2.3里,protoc—py_out命令只生成原生的Python代码。 尽管PB(Protocol Buffers)可以为C++语言生成快速解析和序列化代码,但是这种方式对于Python不适用,...
本套PPT,拥有以下OC语言的内容: 一、OC的三大特性(封装、继承、多态) 二、OC的特有语法(点语法、@property、@synthesize关键字、id、构造方法、分类、...四、协议(protocol)代码块(block) 五、ARC 等等
1251- Client does not support authentication protocol …的错误,网上查了一下 ,某位前辈直接给出了答案,特此记录,以为成长道路上留下足迹… 原因: mysql 8.0之前的版本的加密规则与8.0不同,之前是mysql_...
做了一下mysql升级,开发机上mysql升级到5.6版本,结果连接一些低版本的mysql服务器报出如下异常:Warning: mysql_connect(): Connection using old (pre-4.1.1) authentication protocol refused (client option ...
东半球最高效的Protocol <=>服务中间件,解决中间件的占用内存问题。 支持:OC / Swift / OC + Swift 映射和缓存 安全模式 完善的中间件组件化示例工程 使用示例【Swift / ObjC】 业界通用的组件通信方案 URL...
java安卓仿微信聊天软件源码 目录 工具 备注栏 :link: 或 :memo: 链接详细介绍或最佳实践。其中 :link: - 指外链接,:memo: - 指 Wiki 可编辑 项目 ...OC) ...||Protocol Buffers 的 Swift 语言实现库
过4.3,避免2.3.1,混淆结果接近手工翻新,支持OC所有语法的差异化,支持文件名、类、分类、方法、属性、Protocol、宏、Block、静态变量、枚举等符号的差异化,支持png、jpg、plist、bundle、xib、storyborad等资源关联...
1、拉取最新的mysql:docker pull mysql 2、配置端口映射以及密码启动mysql:docker run ‐p 3306:3306 ‐‐name ...4、mysql -u root -p 输入密码后,进入mysql命令窗口 5、依次输入以下命令: ALTER USER ‘root’@