Protocol delegate

协议,是通过网络,计算机使用者进行通讯后,互相进行约定规定的集合。两个类进行通
讯,用协议就比较方便。下面是 CocoaChina 版主“angellixf”为新手写的协议入门介绍
以及代码例子,希望对刚入门开发者有所帮助
一、说明
1.协议声明了可以被任何类实现的方法
2.协议不是类,它是定义了一个其他对象可以实现的接口
3.如果在某个类中实现了协议中的某个方法,也就是这个类实现了那个协议。
4.协议经常用来实现委托对象。一个委托对象是一种用来协同或者代表其他对象的特殊对
象。
5:委托,就是调用自己定义方法,别的类来实现。
6.新特性说明
@optional 预编译指令:表示可以选择实现的方法
@required 预编译指令:表示必须强制实现的方法
二、定义
.h
@protocol ContactCtrlDelegate
-(void)DismissContactsCtrl;
@end
@interface ContactsCtrl : UIViewController {
id <ContactCtrlDelegate> delegate;
}
@property (nonatomic, assign) id <ContactCtrlDelegate> delegate;
.m
@synthesize delegate;
三、例子
例如:UITextView
@protocol UITextViewDelegate <NSObject>
@optional
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
- (BOOL)textViewShouldEndEditing:(UITextView *)textView;
- (void)textViewDidBeginEditing:(UITextView *)textView;
- (void)textViewDidEndEditing:(UITextView *)textView;
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text;
- (void)textViewDidChange:(UITextView *)textView;
- (void)textViewDidChangeSelection:(UITextView *)textView;
@end
如果要调用以上这些方法,就必须设置 UITextView 的委托:TextView.delegate = self;
四、Demo
1、ContactsCtrl.h
#import <UIKit/UIKit.h>
//定义协议
@protocol ContactCtrlDelegate
-(void)DismissContactsCtrl;
@end
@interface ContactsCtrl : UIViewController {
IBOutlet UINavigationBar *ContactNavBar;
id <ContactCtrlDelegate> delegate;
}
@property (nonatomic, assign) id <ContactCtrlDelegate> delegate;
-(IBAction)canCelBtn:(id)sender;
@end
2、ContactsCtrl.m
@implementation ContactsCtrl
@synthesize delegate;
// Implement viewDidLoad to do additional setup after loading the view, typical
ly from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
ContactNavBar.topItem.prompt = @"选取联系人发送短信";
}
//调用协议中的方法
-(IBAction)canCelBtn:(id)sender{
[delegate DismissContactsCtrl];
}
3、ProtocolDemoCtrl.h
#import <UIKit/UIKit.h>
#import "ContactsCtrl.h"
@interface ProtocolDemoCtrl : UIViewController <ContactCtrlDelegate>{//添加委托
ContactsCtrl *contactsView;
}
4、ProtocolDemoCtrl.m
#import "ProtocolDemoCtrl.h"
#define BARBUTTONADD(SELECTOR) [[[UIBarButtonItem alloc] initWithBarButtonSyste
mItem:UIBarButtonSystemItemAdd target:self action:SELECTOR] autorelease];
@implementation ProtocolDemoCtrl
@synthesize contactsView;
// Implement viewDidLoad to do additional setup after loading the view, typical
ly from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = BARBUTTONADD(@selector(addContactA
ction:));
}
- (void)addContactAction:(id)sender{
ContactsCtrl *contactView = [[ContactsCtrl alloc] initWithNibName:@"Contact
sCtrl" bundle:nil];
self.contactsView = contactView;
contactsView.delegate = self;//设置委托
[self presentModalViewController:contactsView animated:YES];
[contactView release];
}
//实现 ContactCtrlDelegate 协议中的方法
-(void)DismissContactsCtrl{
[contactsView dismissModalViewControllerAnimated:YES];

}


委托代理,顾名思义,把某个对象要做的事情委托给别的对象去做。
 那么别的对象就是这个对象的代理,代替它来打理要做的事。
 反映到程序中,首先要明确一个对象的委托方是哪个对象,委托所做的内容是什么。
 委托机制在很多语言中都用到的,这只是个通用的思想,网上会有很多关于这方面的介绍。
 那么在苹果开发过程中,用到委托的程序实现思想如下,我主要拿如何在视图之间传输信息做个例子。
 譬如:在两个页面(UIIview视图对象)实现传值,用委托(delegate)可以很好做到!
 方法:
 @interface A:UIView
 id transparendValueDelegate;
 @property(nomatic, retain) id transparendValueDelegate;
 @end

 @implemtion A
 @synthesize transparendValueDelegate


 -(void)Function
 
 NSString* value @"hello";
 [transparendValueDelegate transparendValue: value];
 }
 @end

 类B
 @interface B:UIView
 NSString* value;
 @end

 @implemtion B
 -(void)transparendValue:(NSString*)fromValue
 {
 value fromValue;
 NSLog(@"the value is %@ ",value); 
 }
 @end

 //下面的设置A代理委托对象为B
 //在定义A和B类对象处:
 A* [[A alloc] init];
 B* [[B alloc] init];
 a. transparendValueDelegate b;//设置A代理委托对象为B
 这样在视图A和B之间可以通过委托来传值


读取 全局的delegate:
id appdelegate=[[UIApplication sharedApplication] delegate];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值