【Objective-C学习-7】Protocol

Protocol简单来说就是一系列不属于任何类的方法列表,其中声明的方法可以被任何类实现。这种模式一般称为代理(delegation)模式。在iOS和OS X开发中,Apple采用了大量的代理模式来实现MVC中View和Controller的解耦。

定义Protocol很简单,在.h文件中通过关键字@protocol定义,然后给出Protocol的名称,方法列表,然后用@end表示Protocol结束。

下面我们定义一个Button类,该类有一个ButtonDelegate协议,该协议有一个按钮被点击的方法;

然后再定义一个ButtonListener类,该类用来监听显示哪个按钮被点击。

//Button.h
#import <Foundation/Foundation.h>

//Button前置声明
@class Button;

//ButtonDelegate协议继承NSObject协议
@protocol ButtonDelegate <NSObject>

- (void)onClick:(Button *)btn;

@end

@interface Button : NSObject

//delegate是一个id类型,该类型必须实现ButtonDelegate协议
@property (nonatomic, retain) id<ButtonDelegate> delegate;

- (void)click;

@end

//Button.m
#import "Button.h"

@implementation Button

- (void)dealloc {
    [_delegate release];
    [super dealloc];
}

- (void)click {
    //按钮被点击时调用,通知监听器
    if ([_delegate respondsToSelector:@selector(onClick:)]) {
        [_delegate onClick:self];
    } else {
        NSLog(@"不知道哪个按钮被点击了,因为监听器没有实现onClick:方法");
    }
}

@end

//ButtonListener.h
#import <Foundation/Foundation.h>

//protocol前置声明
@protocol ButtonDelegate;

//<ButtonDelegate>表示ButtonListener要实现ButtonDelegate协议
@interface ButtonListener : NSObject <ButtonDelegate>

@end;

//ButtonListener.m
#import "ButtonListener.h"
#import "Button.h"

@implementation ButtonListener

- (void)onClick:(Button *)btn {
    NSLog(@"按键%@被点击了", btn);
}

@end

//main.m
#import <Foundation/Foundation.h>
#import "Button.h"
#import "ButtonListener.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        ButtonListener *btnlistener = [[[ButtonListener alloc] init] autorelease];
        
        Button *button = [[[Button alloc] init] autorelease];
        button.delegate = btnlistener;
        
        Button *button2 = [[[Button alloc] init] autorelease];
        button2.delegate = btnlistener;
        
        [button click];
        [button2 click];
    }
    return 0;
}

Protocol还有一些可选的关键字如@optional,@required用来修饰协议的方法,协议中的方法默认是用@required修饰的,表示要实现该协议的类必须实现该方法,而用@optional修饰的方法则是可选的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值