IOS-协议
2014-04-14 10:22
330 查看
----------------------------MainViewController.m
#import "MainViewController.h"
//导入类
#import "BossView.h"
@interface MainViewController ()
@end
@implementation MainViewController
//每个类都必须写dealloc(在mrc模式xia)
-(void)dealloc{
[super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
BossView * boss = [[BossView alloc] initWithFrame:CGRectMake(100, 100, 100,100)];
[boss setBackgroundColor:[UIColor blackColor]];
// 调用协议实现的方法
boss.delegate = self;
[self.view addSubview:boss];
[boss release];
}
//协议中的方法
- (void)doSomeThing{
NSLog(@"%s",__func__);
}
@end
----------------------------MainViewController.h声明
#import <UIKit/UIKit.h>
//记得导入协议
#import "ProtocolFirst.h"
//记得添加协议
@interface MainViewController : UIViewController<ProtocolFirst>
@end
----------------------------MainViewController.m
#import "BossView.h"
@implementation BossView
-(void)dealloc{
[super dealloc];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 设置按钮
UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50,50)];
[button setBackgroundColor:[UIColor redColor]];
[button addTarget:self action:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside ];
[self addSubview:button];
[button release];
}
return self;
}
// 用属性传进来的类(符合定义的协议的)
-(void)buttonAction:(id)sender{
// 判断是否响应了协议中的方法
if ([self.delegate respondsToSelector:@selector(doSomeThing)]) {
// 调用协议中的方法
[self.delegate doSomeThing];
}
}
@end
----------------------------MainViewController.h
#import <UIKit/UIKit.h>
//添加协议
#import "ProtocolFirst.h"
@interface BossView : UIView
//添加
@property(nonatomic,retain) id<ProtocolFirst> delegate;
@end
#import "MainViewController.h"
//导入类
#import "BossView.h"
@interface MainViewController ()
@end
@implementation MainViewController
//每个类都必须写dealloc(在mrc模式xia)
-(void)dealloc{
[super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
BossView * boss = [[BossView alloc] initWithFrame:CGRectMake(100, 100, 100,100)];
[boss setBackgroundColor:[UIColor blackColor]];
// 调用协议实现的方法
boss.delegate = self;
[self.view addSubview:boss];
[boss release];
}
//协议中的方法
- (void)doSomeThing{
NSLog(@"%s",__func__);
}
@end
----------------------------MainViewController.h声明
#import <UIKit/UIKit.h>
//记得导入协议
#import "ProtocolFirst.h"
//记得添加协议
@interface MainViewController : UIViewController<ProtocolFirst>
@end
----------------------------MainViewController.m
#import "BossView.h"
@implementation BossView
-(void)dealloc{
[super dealloc];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 设置按钮
UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50,50)];
[button setBackgroundColor:[UIColor redColor]];
[button addTarget:self action:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside ];
[self addSubview:button];
[button release];
}
return self;
}
// 用属性传进来的类(符合定义的协议的)
-(void)buttonAction:(id)sender{
// 判断是否响应了协议中的方法
if ([self.delegate respondsToSelector:@selector(doSomeThing)]) {
// 调用协议中的方法
[self.delegate doSomeThing];
}
}
@end
----------------------------MainViewController.h
#import <UIKit/UIKit.h>
//添加协议
#import "ProtocolFirst.h"
@interface BossView : UIView
//添加
@property(nonatomic,retain) id<ProtocolFirst> delegate;
@end
相关文章推荐
- iOS网络编程TCP/IP应用篇(三)-协议的定制
- IOS开发协议使用之──非正式协议和正式协议
- 【IOS 开发】Objective - C 面向对象高级特性 - 包装类 | 类处理 | 类别 | 扩展 | 协议 | 委托 | 异常处理 | 反射
- iOS 页面传值方式 单例,block,代理协议方法,通知中心
- ios委托delegate与协议protocol
- iOS基础控件-UITabBarController - 3 之<UITabBarControllerDelegate>协议中的四个重要方法
- iOS MQTT 协议应用
- iOS中协议的声明以及使用
- iOS开发-Protocol协议及委托代理(Delegate)传值
- ios协议与委托
- ios Nscoding协议 序列化 便于磁盘保存
- iOS上传文件,有关http上传协议-RFC1867
- iOS调用SOAP协议的WebServices接口
- IOS代理协议与委托
- IOS学习之OC语言BLOCK和协议
- IOS 杂笔-2(协议)
- 【iOS开发技术分享】协议与代理
- iOS ---利用文本框的协议判断是否输入内容
- Mqtt协议IOS端移植1
- 常见iOS面试题 之 怎么判断一个类是否遵循某个协议