【IOS学习】之四、协议,委托,分类粗解
何为协议,何为委托,何为分类(类别)?
委托 即 代理 delegate:
- @interface A: UIView
- @property(nonatic, retain) id aValueDelegate;
- @end;
- @implementation A
- - (void) fa
- {
- NSString *value = @"hello";
- [aValueDelegate aValue:value];
- }
- @end;
- @interface B: UIView
- NSString *value;
- @end;
- @implementation B
- - (void) aValue:(NSString *)fromValue
- {
- value = fromValue;
- NSLog(@"%@", value);
- }
- @end;
- A *a = [[A alloc] init];
- B *b = [[B alloc] init];
- a.aValueDelegate = b; //设置a代理委托对象为b
- @interface NSString(NumberConvenience)
- -(NSNumber*) lengthAsNumber;
- @end
- @interface NSObject (***Delegate)
- //method
- @end
- //
- // Dog.h
- // protocol2
- //
- // Created by peter on 14-2-25.
- // Copyright (c) 2014年 peter. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- @interface Dog : NSObject
- @property int ID;
- @end
- @interface NSObject(myCategory)
- - (void)callFromNSObject;
- @end
- //
- // Dog.m
- // protocol2
- //
- // Created by peter on 14-2-25.
- // Copyright (c) 2014年 peter. All rights reserved.
- //
- #import "Dog.h"
- @implementation Dog
- - (id)init
- {
- self = [super init];
- return self;
- }
- @end
- @implementation NSObject(myCategory)
- - (void)callFromNSObject
- {
- NSLog(@"iam nsobject");
- }
- @end
- //
- // Person.h
- // protocol2
- //
- // Created by peter on 14-2-25.
- // Copyright (c) 2014年 peter. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import "Dog.h"
- @interface Person : NSObject
- //{
- // Dog *dog;
- //}
- @property Dog *dog;
- - (void)callFun;
- @end
- //
- // Person.m
- // protocol2
- //
- // Created by peter on 14-2-25.
- // Copyright (c) 2014年 peter. All rights reserved.
- //
- #import "Person.h"
- @implementation Person
- @synthesize dog;
- - (void) callFun
- {
- NSLog(@"Call Fun!");
- [dog callFromNSObject];
- }
- @end
- //
- // main.m
- // protocol2
- //
- // Created by peter on 14-2-25.
- // Copyright (c) 2014年 peter. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import "Dog.h"
- #import "Person.h"
- int main(int argc, const charchar * argv[])
- {
- @autoreleasepool {
- Dog *dog = [[Dog alloc]init];
- [dog setID:10];
- Person *qy = [[Person alloc]init];
- [qy setDog:dog];
- [qy callFun];
- }
- return 0;
- }
- @protocol NSCopying
- -(id) copyWithZone:(NSZone *) zone;
- @end
- @interface Car : NSObject<NSCopying>
- {
- // instance variable
- }
- // method
- @end // Car
2.协议不是类,它是定义了一个其他对象可以实现的接口
3.如果在某个类中实现了协议中的某个方法,也就是这个类实现了那个协议。
4.协议经常用来实现委托对象。一个委托对象是一种用来协同或者代表其他对象的特殊对象。
5:委托,就是调用自己定义方法,别的类来实现。
6.新特性说明
@optional预编译指令:表示可以选择实现的方法
@required预编译指令:表示必须强制实现的方法
- @protocol myprotocol <NSObject>
- @optional
- -(void)print:(int)value;
- //可选的方法
- @required
- -(int)printValue:(int)value1 andValue:(int)value2;
- //必须实现的
- @end
- #import <Foundation/Foundation.h>
- #import "myprotocol.h"
- //实现协议 myprotocol
- @interface mytest : NSObject<myprotocol>
- - (void)showInfo;
- @end
- #import "mytest.h"
- @implementation mytest
- -(void)showInfo
- {
- NSLog(@"I am in showInfo");
- }
- //实现协议必须实现的
- -(int)printValue:(int)value1 andValue:(int)value2
- {
- NSLog(@"print value1 %d,value2 %d",value1,value2);
- return 0;
- }
- //实现可选的
- -(void)print:(int)value
- {
- NSLog(@"print value is %d",value);
- }
- @end
- #import <Foundation/Foundation.h>
- #import "mytest.h"
- #import "myprotocol.h"
- int main (int argc, const charchar * argv[]) {
- @autoreleasepool {
- mytest *test=[[mytest alloc]init];
- [test showInfo];
- [test printValue:20 andValue:30];
- //print协议是可选的,所以在用之前一定要判断是否实现了,不然可能会出错,使用下面的方法
- // [test print:20];
- SEL sel=@selector(print:);
- if([test respondsToSelector:sel]){
- [test print:11];
- }
- //用协议的方式实现
- id<myprotocol> protocol =[[[mytest alloc]init]autorelease];
- [protocol showInfo];
- [protocol printValue:200 andValue:300];
- if([protocol respondsToSelector:@selector(print:)]){
- [protocol print:111];
- }
- }
- return 0;
- }
- //
- // Dog.h
- // catagory
- //
- // Created by peter on 14-2-25.
- // Copyright (c) 2014年 peter. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- @protocol dogBark;
- @interface Dog : NSObject
- {
- int barkCount;
- NSTimer *timer;
- }
- @property int ID;
- @property (assign)id<dogBark> delegate; //dog master
- @end
- @protocol dogBark <NSObject>
- - (void)bark: (Dog*)thisDog count:(int)count;
- @end
- //
- // Dog.m
- // catagory
- //
- // Created by peter on 14-2-25.
- // Copyright (c) 2014年 peter. All rights reserved.
- //
- #import "Dog.h"
- @implementation Dog
- //ID use _ID
- @synthesize delegate;
- - (id)init
- {
- if (self = [super init]) {
- //create nstimer user, 1.0s use updateTimer:nil
- timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
- }
- return self;
- }
- - (void) updateTimer:(id)arg
- {
- barkCount++;
- NSLog(@"dog bar %d", barkCount);
- //user master delegate bark:count
- [delegate bark:self count:barkCount];
- }
- @end
- //
- // person.h
- // catagory
- //
- // Created by peter on 14-2-25.
- // Copyright (c) 2014年 peter. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import "Dog.h"
- @interface person : NSObject<dogBark>
- {
- Dog *_dog;
- }
- @property (retain, nonatomic) Dog *dog;
- @end
- //
- // person.m
- // catagory
- //
- // Created by peter on 14-2-25.
- // Copyright (c) 2014年 peter. All rights reserved.
- //
- #import "person.h"
- @implementation person
- - (void)setDog:(Dog *)aDog
- {
- if (_dog != aDog) {
- [_dog setDelegate:self];
- }
- }
- //dog use person interface
- - (void)bark:(Dog *)thisDog count:(int)count
- {
- NSLog(@"person bark: this dog %d bark %d", [thisDog ID], count);
- }
- @end
- //
- // main.m
- // catagory
- //
- // Created by peter on 14-2-25.
- // Copyright (c) 2014年 peter. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import "Dog.h"
- #import "person.h"
- int main(int argc, const charchar * argv[])
- {
- @autoreleasepool {
- person *qy = [[person alloc]init];
- Dog *dog = [[Dog alloc]init];
- [dog setID:10];
- [qy setDog:dog];
- while (1) {
- [[NSRunLoop currentRunLoop]run];
- }
- }
- return 0;
- }
解释一下循环:
【IOS学习】之四、协议,委托,分类粗解的更多相关文章
- iOS学习——(转)UIResponder详解
本文转载自:ios开发 之 UIResponder详解 我们知道UIResponder是所有视图View的基类,在iOS中UIResponder类是专门用来响应用户的操作处理各种事件的,包括触摸事件( ...
- iOS学习——(转)NSObject详解
本文主要转载自:ios开发 之 NSObject详解 NSObject是大部分Objective-C类继承体系的根类.这个类遵循NSObject协议,提供了一些通用的方法,对象通过继承NSObject ...
- iOS学习——iOS项目Project 和 Targets配置详解
最近开始学习完整iOS项目的开发流程和思路,在实际的项目开发过程中,我们通常需要对项目代码和资料进行版本控制和管理,一般比较常用的SVN或者Github进行代码版本控制和项目管理.我们iOS项目的开发 ...
- iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem
http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINavigati ...
- [转]iOS学习之UINavigationController详解与使用(三)ToolBar
转载地址:http://blog.csdn.net/totogo2010/article/details/7682641 iOS学习之UINavigationController详解与使用(二)页面切 ...
- [转]iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController
转载地址:http://blog.csdn.net/totogo2010/article/details/7682433 iOS学习之UINavigationController详解与使用(一)添加U ...
- IOS中类的扩展(协议,分类)
IOS中类的扩展(协议,分类) 扩展类,我们可以使用协议和分类这两种方法,下面我们来分别实现这两种方法: 参考网址:http://www.cnblogs.com/wendingding/p/37095 ...
- iOS学习之UINavigationController详解与使用(三)ToolBar
1.显示Toolbar 在RootViewController.m的- (void)viewDidLoad方法中添加代码,这样Toobar就显示出来了. [cpp] view plaincopy [ ...
- iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController
iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem是上篇,我们接着讲UINavigationController的重要作用,页面的管理和切换. ...
随机推荐
- CDB和PDB的创建、连接、启动、关闭
一.CDB和PDB基本管理 基本概念: Multitenant Environment:多租户环境 CDB(Container Database):数据库容器 PD(Pluggable Databas ...
- V-rep学习笔记:视觉传感器1
Vision sensors, which can detect renderable entities(Renderable objects are objects that can be seen ...
- NTCPMSG 开源高性能TCP消息发送组件
https://www.cnblogs.com/eaglet/archive/2013/01/07/2849010.html 目前的.net 架构下缺乏高效的TCP消息发送组件,而这种组件是构建高性能 ...
- Arduino通过I2C(PCF8574T)驱动1602LCD
Arduino中使用I2C通信可直接调用Wire.h库, 这个库允许Arduino链接其他I2C设备, 链接线有两条, 分别是SDA(数据行)和SCI(时钟线). 各型号Arduino的I2C对应引脚 ...
- 浅谈ELK日志分析平台
作者:珂珂链接:https://zhuanlan.zhihu.com/p/22104361来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 小编的话 “技术干货”系列文章 ...
- poj 2632 Crashing Robots(模拟)
链接:poj 2632 题意:在n*m的房间有num个机器,它们的坐标和方向已知,现给定一些指令及机器k运行的次数, L代表机器方向向左旋转90°,R代表机器方向向右旋转90°,F表示前进,每次前进一 ...
- 解决打开bootstrap模态框抖动问题
//打开模态框 function modalOpen(){ $('body').css("overflow", "hidden"); } //关闭模态框 fun ...
- ios中在uiNavigationcontroller中做转场动画
1:了解,当创建一个UINavigationcontroller中时,当创建一个子视图控制器压入uiNavigationController中,其中里面的view也别加入UINavigationcon ...
- Linux-LVS为何不能完全替代DNS轮询
转自:链接 上一篇文章“一分钟了解负载均衡的一切”引起了不少同学的关注,评论中大家争论的比较多的一个技术点是接入层负载均衡技术,部分同学持这样的观点: 1)nginx前端加入lvs和keepalive ...
- boost asio resolver
asio 中的resolver一般用于将host等信息转化为socket信息,类似于getaddrinfo() 以下代码 boost::asio::io_service io; tcp::resolv ...