在Objective-C中实现Delegate:协议
来源:互联网 发布:hp61墨盒清零软件 编辑:程序博客网 时间:2024/04/30 04:22
在Objective-C中实现Delegate:协议
协议的作用:
实现两个类之间的通讯。
协议的特性和注意事项:
- 协议声明了可以被任何类实现的方法;
- 协议声明的方法前加:@optional表示可选,加@required表示必须实现;
- 协议不是类,而是一种两个类之间的接口;
- 定义了协议的类可以看做是将协议定义的方法代理给了实现它们的类;
协议的定义:
一个在标准的Foundation头文件NSObject.h中定义NSCopying协议的例子:
NSObject.h@protocol NSCopying- (id) copyWithZone: (NSZone *) zone;@end
声明一个类采用了一个协议时在@interface行后尖括号内列出协议名称:
@interface AddressBook: NSObject <NSCopying>
AddressBook是NSObject的子类,并且它遵守NSCopying协议。不必在AddressBook类的接口部分声明NSCopying的方法,但需要在实现部分去实现。
协议的实例:
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
ContactsCtrl.m
@implementation ContactsCtrl@synthesize delegate;// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.- (void)viewDidLoad { [super viewDidLoad]; ContactNavBar.topItem.prompt = @"选取联系人发送短信";}-(IBAction)canCelBtn:(id)sender{ [delegate DismissContactsCtrl];}
ProtocolDemoCtrl.h
#import <UIKit/UIKit.h>#import "ContactsCtrl.h"@interface ProtocolDemoCtrl : UIViewController <ContactCtrlDelegate>{ ContactsCtrl *contactsView;}
ProtocolDemoCtrl.m
#import "ProtocolDemoCtrl.h"#define BARBUTTONADD(SELECTOR) [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:SELECTOR] autorelease];@implementation ProtocolDemoCtrl@synthesize contactsView;// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.- (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.rightBarButtonItem = BARBUTTONADD(@selector(addContactAction:));}- (void)addContactAction:(id)sender{ ContactsCtrl *contactView = [[ContactsCtrl alloc] initWithNibName:@"ContactsCtrl" bundle:nil]; self.contactsView = contactView; contactsView.delegate = self;//设置委托 [self presentModalViewController:contactsView animated:YES]; [contactView release]; }-(void)DismissContactsCtrl{ [contactsView dismissModalViewControllerAnimated:YES];}
0 0
- 在Objective-C中实现Delegate:协议
- 在 Objective-C 中实现协议扩展
- Objective-C中的协议(procotol,delegate)
- Objective-C 中协议
- Objective-C中的“委托(delegate)”和“协议(protocal)”
- objective-c delegate
- objective-c - delegate
- Objective-C delegate理解
- objective-c - delegate
- objective-c delegate
- Objective-C中Delegate,Protocol与Interface的区别
- Objective-c中的delegate浅析
- Objective-C 协议 简介 +委托的实现
- Objective-C 协议 简介 +委托的实现
- Objective-C 协议 简介 +委托的实现
- Objective-C 协议 简介 +委托的实现
- Objective-C 协议 简介 +委托的实现
- 在C++中实现委托(Delegate)
- MySQL编译安装详解
- 在安卓开发时,鼠标停留,却无法显示帮助文档,显示This element neither has attached source nor attached Javadoc
- 汉诺塔问题(递归过程解析)
- 帧、行、场
- 《卓越程序员密码》
- 在Objective-C中实现Delegate:协议
- Let us learn C in Code <10>
- 如何设置控件的背景为透明的
- NYOJ 6 喷水装置(一)
- ShowMessage或者Messagebox弹出后出现access violation......
- poj1338
- redis的简单理解
- mac上安装brew出现的问题
- 基于android的远程视频监控系统