zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    进程同步&&进程互斥
    CHAP4 字符串和格式化输入输出
    记录学习到的内容
    数据链路层 差错控制
    二叉树的顺序存储
    Java复习笔记
    Stream流
    函数式接口
    网络编程
    接口,多态,抽象类总结
  • 原文地址:https://www.cnblogs.com/xujinzhong/p/8676179.html
Copyright © 2011-2022 走看看