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

转载于:https://www.cnblogs.com/xujinzhong/p/8676179.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值