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

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. UT源码116

    2)NextDate函数问题 NextDate函数说明一种复杂的关系,即输入变量之间逻辑关系的复杂性 NextDate函数包含三个变量month.day和year,函数的输出为输入日期后一天的日期. ...

  2. 怎么触发gridview 的SelectedIndexChanged事件?

    <asp:GridView onclick="javascript:SelectedIndexChanged()" ID="GridView1" runa ...

  3. 托管调试助手 "PInvokeStackImbalance":的调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管

    在C#中一定要检查引用时的数据类型 WinAPI 的数据类型 默认是32位的,但是引用时外部的是 Long类型默认是64位的.所以引用时需要将 long 改为 int 型. 参照 http://blo ...

  4. 【leetcode 106. 从中序与后序遍历序列构造二叉树】解题报告

    前往 中序,后序遍历构造二叉树, 中序,前序遍历构造二叉树 TreeNode* build(vector<int>& inorder, int l1, int r1, vector ...

  5. [hdu 1671] Phone List - Trie

    Given a list of phone numbers, determine if it is consistent in the sense that no number is the pref ...

  6. Hadoop eclipse plugin

    我的eclipse是在win7上,hadoop在win7里的虚拟机里的ubuntu上,为了方便起见,想在eclipse上安装hadoop的插件,主要参考 https://my.oschina.net/ ...

  7. 51nod1315(位运算)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1315 题意:中文题诶- 思路:位或(|)运算是二进制位有一个 ...

  8. Spring MVC 参数的绑定方法

    在Spring MVC中,常见的应用场景就是给请求的Url绑定参数.本篇就介绍两种最最基本的绑定参数的方式: 基于@RequestParam 这种方法一般用于在URL后使用?添加参数,比如: @Req ...

  9. 通过ps给透明通道的图片添加灰度(适用于需要兼容IE7,效果很好)

    原始的图片是这样的 第一步: 第二步: 第三步: 第四步: 更多(文字居中): 1: 2: 3: 4:

  10. Eclipse设置每行代码的长度

    打开 Window -> preferences -> java -> code style -> formatter -> edit -> line wrappi ...