在实际开发中,协议的应用非常广泛,以下是实际应用的例子。

1、协议的定义:

myProtocolDelegate.h

//
// myProtocolDelegate.h
// zlwPlayerApplication
//
// Created by xjz on 2018/3/30.
// Copyright © 2018年 xujinzhong. All rights reserved.
// #import <Foundation/Foundation.h> // 协议定义
@protocol SampleProtocolDelegate <NSObject> @required
- (void) processCompleted; @end @interface myProtocolDelegate : NSObject
{
// Delegate to respond back
id <SampleProtocolDelegate> _delegate;
} @property (nonatomic,strong) id delegate; -(void)startSampleProcess; // Instance method @end

myProtocolDelegate.m

//
// myProtocolDelegate.m
// zlwPlayerApplication
//
// Created by xjz on 2018/3/30.
// Copyright © 2018年 xujinzhong. All rights reserved.
// #import "myProtocolDelegate.h" @implementation myProtocolDelegate -(void)startSampleProcess{
if ([self.delegate respondsToSelector:@selector(processCompleted)]) {
[self.delegate
processCompleted];
}

} @end

2、协议的调用和实现

ViewController.h

//
// ViewController.h
// zlwPlayerApplication
//
// Created by xjz on 2018/1/31.
// Copyright © 2018年 xujinzhong. All rights reserved.
// #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end

ViewController.m

//
// ViewController.m
// zlwPlayerApplication
//
// Created by xjz on 2018/1/31.
// Copyright © 2018年 xujinzhong. All rights reserved.
// #import "ViewController.h"
#import "Masonry.h"
#import "ReactiveObjC.h"
#import "myProtocolDelegate.h" @interface ViewController ()<SampleProtocolDelegate> @property(nonatomic, strong) UIButton *btnDone;
@property(nonatomic, strong) UILabel *lableMsg; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; myProtocolDelegate *myDelegate = [[myProtocolDelegate alloc] init];
myDelegate.
delegate = self; self.lableMsg.text = @"显示内容"; [[self.btnDone rac_signalForControlEvents:UIControlEventTouchDown] subscribeNext:^(__kindof UIControl * _Nullable x) {
[myDelegate startSampleProcess];
}];
} -(UIButton *)btnDone{
if (!_btnDone) {
_btnDone = [UIButton new];
_btnDone.backgroundColor = [UIColor grayColor];
_btnDone.layer.cornerRadius = .f;
_btnDone.layer.masksToBounds = YES;
[_btnDone setTitle:@"Done" forState:UIControlStateNormal];
[self.view addSubview:_btnDone]; [_btnDone mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.width.offset();
make.height.offset();
}];
}
return _btnDone;
} -(UILabel *)lableMsg{
if (!_lableMsg) {
_lableMsg = [UILabel new];
_lableMsg.font = [UIFont systemFontOfSize:.f];
_lableMsg.textColor = [UIColor redColor];
_lableMsg.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:_lableMsg]; [_lableMsg mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.btnDone.mas_top).offset(-);
make.centerX.equalTo(self.view);
make.width.equalTo(self.view);
make.height.offset();
}];
}
return _lableMsg;
} #pragma mark - Sample protocol delegate
-(void)processCompleted{
static NSInteger idx = ;
self.lableMsg.text = [NSString stringWithFormat:@"代理-%zi", idx++];
} @end

iOS - 协议实现的例子的更多相关文章

  1. JAVA基础知识之网络编程——-基于UDP协议的通信例子

    UDP是一种不可靠的协议,它在通信两端各建立一个socket,这两个socket不会建立持久的通信连接,只会单方面向对方发送数据,不检查发送结果. java中基于UDP协议的通信使用DatagramS ...

  2. iOS协议

    ios中的协议:大家猛一看 感觉挺高深的  其实ios中的协议就是c#,java中的接口 只是变了一个形式: 自我感觉ios中的协议没有c#中的接口好  人家的接口就是固定你的程序内容的  而ios中 ...

  3. ios 协议分析

    1 基本用途 可以用来声明一大堆方法(不能声明成员变量) 只要某个类遵守了这个协议,就相当于拥有了这个协议中的所有方法声明 只要父类遵守了某个协议,就相当于子类也遵守了 2 格式 协议的编写 @pro ...

  4. iOS 7新功能例子

    参考https://github.com/shu223/iOS7-Sampler Code examples for the new functions of iOS 7. Contents Dyna ...

  5. iOS 协议

    协议分为三部分:声明.引用.实现. 通常,声明协议和声明协议类型的属性都是在同一个类中.声明协议和声明协议作为属性在头文件中,引用在声明类的实现文件中.而实现协议则在其它类中.

  6. ios协议和委托

    在iPhone开发协议和委托是常接触到的东西,到底什么是协议什么是委托,他们什么关系? 一 协议 (1)协议相当于没有与类相关联的接口,他申明一组方法,列出他的参数和返回值,共享给其他类使用,然后不进 ...

  7. iOS 协议分发

    Github:AOMultiproxier.HJProtocolDispatcher 协议实现分发器,能够轻易实现将协议事件分发给多个实现者. 一.AOMultiproxier.h #define A ...

  8. ios协议调起app

    function openIos(url, callback) { if (!url) { return; } var node = document.createElement('iframe'); ...

  9. iOS delegate, 代理/委托与协议.

    之前知知道iOS协议怎么写, 以为真的跟特么java接口一样, 后来发现完全不是. 首先, 说说应用场景, 就是当你要用一个程序类, 或者说逻辑类, 去控制一个storyboard里面的label, ...

随机推荐

  1. p2371&bzoj2118 墨墨的等式

    传送门(bzoj) 题目 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存 ...

  2. vue前端+java后端 vue + vuex + koa2开发环境搭建及示例开发

    vue + vuex + koa2开发环境搭建及示例开发 https://segmentfault.com/a/1190000012918518 vue前端+java后端 https://blog.c ...

  3. Entity Framework Code-First(7):Inheritance Strategy

    Inheritance Strategy in Code-First: We have seen in the Code First Conventions section that it creat ...

  4. C#使用Json.NET解析Json

    本文转载自 http://xiaosheng.me/2016/10/01/article25/ 最近在 C# 项目中需要使用到 Json 格式的数据,我简单上网搜索了一下,基本上有两种操作 Json ...

  5. synchronized关键字的作用域

    转自:http://www.cnblogs.com/devinzhang/archive/2011/12/14/2287675.html 1.synchronized关键字的作用域有二种: 1)是某个 ...

  6. Dapper.Common基于Dapper的开源LINQ超轻量扩展

    Dapper.Common Dapper.Common是基于Dapper的LINQ实现,支持.net core,遵循Linq语法规则.链式调用.配置简单.上手快,支持Mysql,Sqlserver(目 ...

  7. C# Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).

    一.问题描述 在做C# 的 Guid 转换时,出现这个问题:Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-x ...

  8. [WIP]php入門

    创建: 2019/06/19 安装  MAMP   变量与运算符  php标签  <?php ... ?> <?php ... ?> ● 在文件最后的 ?> 通常省略, ...

  9. 七层登录——VB.NET版

    敲完三层后,感觉还是对三层架构没有那么亲切,和小伙伴交流后,他们说多多敲几遍,就懂了,其实就是那么几条线,所以:不管会不会,先去做吧! 下面是关于七层的包图: 我的解决方案: 代码部分: U层: &l ...

  10. ie-"此更新不适应于此电脑"

    cmd-dos命令 expand –F:* C:\update\Windows6.1-KB2533623-x64.msu C:\update\ dism.exe /online /Add-Packag ...