Objective-c中的delegate浅析
delegate初探
在ios开发中,我们常常会用到类似例如以下的对话框:

因此,例如以下这段代码我们也就非常熟悉了:
- (IBAction)showSheet:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@"title,nil时不显示"
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:@"确定"
otherButtonTitles:@"第一项", @"第二项",nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[actionSheet showInView:self.view];
}
当中initWithTitle函数的第二个參数为delegate,那么是什么呢? 我们到它的头文件里看看。
initWithTitle这个函数的声明例如以下 :
- (id)initWithTitle:(NSString *)title delegate:(id<UIActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;
是的,上面这个巨长无比的函数声明就是initWithTitile函数,oc这个语言本身给我的感觉就是繁杂。废话不多说,我们直接看到delegate參数的类型是id<UIActionSheetDelegate>。直接看UIActionSheetDelegate的声明:
@protocol UIActionSheetDelegate <NSObject>
@optional // Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex; // Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button.
// If not defined in the delegate, we simulate a click in the cancel button
- (void)actionSheetCancel:(UIActionSheet *)actionSheet; - (void)willPresentActionSheet:(UIActionSheet *)actionSheet; // before animation and showing view
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet; // after animation - (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex; // before animation and hiding view
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex; // after animation @end
能够看到UIActionSheetDelegate是一个普普通通的协议,在@optional以下有六个函数,这些函数都是可选实现的,每一个函数相应的是UIActionSheet中每一个button的点击事件处理。当然最后两个函数是依照索引来区分Button对象的。
delagate的实现
协议与delegate不是同一概念,协议是语言级别的特性,而delegate是借助协议来的实现的一种设计模式,事实上就是代理模式。通过注入
代理对象实现对应的功能。
事实上它的主要功能也就是实现回调,与Java中的listener一样。
以下以一个演示样例来说明 :
// ButtonClickDelegate协议
@protocol ButtonClickDelegate <NSObject> -(void) onClick: (id) sender ; @end // view的声明。实现了ButtonClickDelegate协议
@interface UIView : NSObject <ButtonClickDelegate >
{
@protected id<ButtonClickDelegate> clickDelegate ;
} // 点击事件代理。因此UIView实现了ButtonClickDelegate协议,因此自己能够为自己代理。
@property (nonatomic, strong) id<ButtonClickDelegate> clickDelegate ;
// 点击view的触发函数
-(void) performClick ; @end // view的实现
@implementation UIView @synthesize clickDelegate ; // 默认的点击事件
-(id) init
{
self = [super init] ;
if ( self ) {
clickDelegate = self ;
}
return self;
} // 点击view的事件的默认处理
-(void) onClick: (id) sender
{
NSLog(@"点击view的默认处理函数.") ;
} // 点击事件
-(void) performClick
{
[clickDelegate onClick: self ] ;
} @end // ViewController声明, 实现了ButtonClickDelegate协议,能够作为UIView的代理
@interface ViewController : NSObject <ButtonClickDelegate>
@property (nonatomic, strong) UIView* parenView ;
@end // ViewController实现
@implementation ViewController -(void) onClick:(id)sender
{
NSLog(@"ViewController来实现点击事件") ;
} @end
main函数:
// main
int main(int argc, const char * argv[])
{ @autoreleasepool {
// view对象
UIView* view = [[UIView alloc] init] ;
[view performClick] ; // 构建一个ViewController对象
ViewController* controller = [[ViewController alloc] init] ;
view.clickDelegate = controller ;
[view performClick] ; }
return 0;
}
首先创建了一个UIView对象view, 然后调用performClick函数,此时view没有设置delegate,可是因为自己实现了ButtonClickDelegate协议,因此能够为自己代理该点击事件。然后我们创建了ViewController对象controller, 而且将该对象设置为view对象的delegate。 然后运行performClick函数,此时在performClick函数中
会运行ViewController中的onClick函数。即controller代理了view的点击事件处理。输出结果例如以下 :
点击view的默认处理函数.
ViewController来实现点击事件
delegate与Java中的Listener
delegate与Java中的Listener的功能大致是同样的,比方我们看看Android中一个button的点击事件的处理。
Button mJumpButton = (Button) findViewById(R.id.button_jump);
mJumpButton.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,
SecondActivity.class);
startActivity(intent);
}
});
当中的Button就相当于上文中的UIView,而其setOnClickListener就相当于delegate属性的设置方法。OnClickListener就扮演了上文中ButtonClickDelegate的角色,
onClick方法更是如出一辙。事实上每一个平台大体上的设计思路也都是非常相近的,观察不同平台的对照实现更easy理解吧。
delegate与id类型
// 点击事件代理,因此UIView实现了ButtonClickDelegate协议。因此自己能够为自己代理。
@property (nonatomic, strong) id<ButtonClickDelegate> clickDelegate ;
注意这里的类型是id<ButtonClickDelegate>;这表明该delegate对象能够是随意类型,可是该类型必须实现ButtonClickDelegate协议,也能够说成该类型必须採用正式协议ButtonClickDelegate。这个就非常像Java中的泛型。比如我们能够在Java中这样使用泛型,
void setData(List<? extends Order> myorders) ;
在setData函数中接受的參数为元素类型为Order子类的List集合,与id<ButtonClickDelegate>是不是又非常相似呢?
Objective-c中的delegate浅析的更多相关文章
- jquery中on/delegate的原理
jquery中on/delegate的原理 早期版本中叫delegate, 后来有过live函数,再后来统一用on.下面的方法等效: // jQuery 1.3 $(selector).(events ...
- 浅谈c#中的delegate和event了
一.开篇忏悔 对自己最拿手的编程语言C#,我想对你说声对不起,因为我到现在为止才明白c#中的delegate和event是怎么用的,惭愧那.好了,那就趁着阳光明媚的早晨简单来谈谈delegate和ev ...
- 理解Objective C 中id
什么是id,与void *的区别 id在Objective C中是一个类型,一个complier所认可的Objective C类型,跟void *是不一样的,比如一个 id userName, 和vo ...
- 浅谈Objective—C中的面向对象特性
Objective-C世界中的面向对象程序设计 面向对象称程序设计可能是现在最常用的程序设计模式.如何开发实际的程序是存在两个派系的-- 面向对象语言--在过去的几十年中,很多的面向对象语言被发明出来 ...
- C#中的Delegate
谈C#中的Delegate http://www.cnblogs.com/hyddd/archive/2009/07/26/1531538.html
- 谈C#中的Delegate
引言 Delegate是Dotnet1.0的时候已经存在的特性了,但由于在实际工作中一直没有机会使用Delegate这个特性,所以一直没有对它作整理.这两天,我再度翻阅了一些关于Delegate的资料 ...
- c#中的delegate(委托)和event(事件)
c#中的delegate(委托)和event(事件) 一.delegate是什么东西? 完全可以把delegate理解成C中的函数指针,它允许你传递一个类A的方法m给另一个类B的对象,使得类B的对象能 ...
- ios中关于delegate(委托)的使用心得
ios中关于delegate(委托)的使用心得 分类: iOS开发2012-05-15 10:54 34793人阅读 评论(9) 收藏 举报 iosuiviewtimerinterfaceprinti ...
- C#中的委托(Delegate)和事件(Event)
原文地址:C#中的委托(Delegate)和事件(Event) 作者:jiyuan51 把C#中的委托(Delegate)和事件(Event)放到现在讲是有目的的:给下次写的设计模式--观察者(Obs ...
随机推荐
- luogu P1592 互质
题目描述 输入两个正整数n和k,求与n互质的第k个正整数. 输入输出格式 输入格式: 仅一行,为两个正整数n(≤10^6)和k(≤10^8). 输出格式: 一个正整数,表示与n互质的第k个正整数. 输 ...
- Beginning Auto Layout Tutorial in iOS 7: Part 2
Auto Layout to the rescue! 接下来就看看如何使用Auto Layout来实现这个效果. 首先移除viewWillLayoutSubviews方法,选择Main.storybo ...
- 【IntelliJ IDEA】升级之后又要激活的解决方法
用了几个月的idea,在它升级之后,又不听话了.启动时候需要重新激活. 解决方法: 1.找到C:\Windows\System32\drivers\etc\下的hosts文件 在文件中添加如下: 0. ...
- C#规范整理·泛型委托事件
基于泛型,我们得以将类型参数化,以便更大范围地进行代码复用.同时,它减少了泛型类及泛型方法中的转型,确保了类型安全.委托本身是一种引用类型,它保存的也是托管堆中对象的引用,只不过这个引用比较特殊,它是 ...
- SilverLight-Access:银光项目测试数据类列表
ylbtech-SilverLight-Access:银光项目测试数据类列表 1.A, Product.cs 产品类 1.A, Product.cs 产品类返回顶部 1,/Access/Product ...
- java基础篇3之反射
1.反射的基础 反射的基石---->Class类 java程序中的各个java类属于同一类事物,描述这类事物的java类名就是Class 获取字节码对应的实例对象(Class类型) class ...
- GlusterFS源代码解析 —— GlusterFS 内存分配方式
原文地址:http://blog.csdn.net/wangyuling1234567890/article/details/24564891 GlusterFS 的内存分配主要有两种方式,一种是内存 ...
- SVN切分支步骤
1.右键project选择Brankch/Tag 2.选择SVN路径并在改路径下填写project名称 3.选择最新版本号 4.填写必要的凝视备忘,方便日后查看 5.刷新父文件夹文件夹.下载被切出来的 ...
- vue sync
1.使用vue cli建立工程 2.在APP.vue中: <template> <div class="details"> <myComponent ...
- IOS知识点收集
17 duplicate symbols for architecture armv7s 用cocoapods 的时候出现,这种错误一般是由重复引用库文件引起. 原因:自己尝试添加Reachabil ...