• iOS


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

    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 = 4.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(100);
                make.height.offset(80);
            }];
        }
        return _btnDone;
    }
    
    -(UILabel *)lableMsg{
        if (!_lableMsg) {
            _lableMsg = [UILabel new];
            _lableMsg.font = [UIFont systemFontOfSize:26.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(-20);
                make.centerX.equalTo(self.view);
                make.width.equalTo(self.view);
                make.height.offset(80);
            }];
        }
        return _lableMsg;
    }
    
    #pragma mark - Sample protocol delegate
    -(void)processCompleted{
        static NSInteger idx = 1;
        self.lableMsg.text = [NSString stringWithFormat:@"代理-%zi", idx++];
    }
    
    @end
  • 相关阅读:
    MySQL主从复制故障1595报错【原创】
    深入理解计算机系统(3.1)------汇编语言和机器语言
    深入理解计算机系统(2.7)------浮点数舍入以及运算
    深入理解计算机系统(2.7)------二进制小数和IEEE浮点标准
    深入理解计算机系统(2.6)------整数的运算
    深入理解计算机系统(2.5)------C语言中的有符号数和无符号数以及扩展和截断数字
    深入理解计算机系统(2.4)------整数的表示(无符号编码和补码编码)
    Spring详解(七)------AOP 注解
    深入理解计算机系统(2.3)------布尔代数以及C语言运算符
    深入理解计算机系统(2.2)------进制间的转换原理
  • 原文地址:https://www.cnblogs.com/xujinzhong/p/8676179.html
Copyright © 2020-2023  润新知