博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
oc协议与代理简单例子
阅读量:4314 次
发布时间:2019-06-06

本文共 969 字,大约阅读时间需要 3 分钟。

创建一个买票协议.该协议规定输出是否还有剩票

1 #import 
2 3 @protocol BuyticketsDelegate
4 5 @required6 - (void) check;7 8 @end
View Code

创建一个代理Agent遵守协议

1 #import "Buytickets.h" 2 @interface Agents : NSObject
3 4 @end 5 6 7 @implementation Agents 8 9 - (void)check{10 NSLog(@"还有剩票");11 }12 13 @end
View Code

创建一个Person类,包含一个遵守协议的代理对象

1 @interface Person : NSObject 2  3 @property(nonatomic, assign)id
delegate; 4 5 - (void)buy; 6 7 @end 8 9 10 #import "Person.h"11 @implementation Person12 13 - (void)buy{14 [_delegate check];15 }16 17 @end
View Code

在main函数里,指定一个代理

1 int main(int argc, const char * argv[]) { 2    @autoreleasepool { 3         4        Person *person = [[Person alloc]init]; 5        Agents *age = [[Agents alloc]init]; 6        person.delegate = age; 7        [person buy]; 8  9         10     }11     return 0;12 }
View Code

 

转载于:https://www.cnblogs.com/wan-huan/p/5727585.html

你可能感兴趣的文章
OCO订单(委托)
查看>>
学习笔记_vnpy实战培训day06
查看>>
回测引擎代码分析流程图
查看>>
Excel 如何制作时间轴
查看>>
matplotlib绘图跳过时间段的处理方案
查看>>
vnpy学习_04回测评价指标的缺陷
查看>>
iOS开发中遇到的问题整理 (一)
查看>>
Linux(SUSE 12)安装jboss4并实现远程访问
查看>>
Neutron在给虚拟机分配网络时,底层是如何实现的?
查看>>
netfilter/iptables全攻略
查看>>
Overlay之VXLAN架构
查看>>
Eclipse : An error occurred while filtering resources(Maven错误提示)
查看>>
在eclipse上用tomcat部署项目404解决方案
查看>>
web.xml 配置中classpath: 与classpath*:的区别
查看>>
suse如何修改ssh端口为2222?
查看>>
详细理解“>/dev/null 2>&1”
查看>>
suse如何创建定时任务?
查看>>
suse搭建ftp服务器方法
查看>>
centos虚拟机设置共享文件夹并通过我的电脑访问[增加smbd端口修改]
查看>>
文件拷贝(IFileOperation::CopyItem)
查看>>