• 10月26日十九大新闻发言人专题新闻发布会 2019-03-22
  • 多交140分“亚裔税”?哈佛大学被控歧视亚裔学生 2019-03-22
  • 男子农田挖到古董抠出吓得魂飞魄散 2019-03-17
  • 瑞典南部城市发生枪击案致5人伤 警方:与恐袭无关 2019-03-10
  • 西安幼升小入学难问题出在哪儿教育部门将统筹协调安排 2019-03-10
  • 传统强队集体慢热(世界杯纵横) 2019-03-10
  • 乌鲁木齐水磨沟区开建两座立体停车库 2019-03-10
  • 山西新闻网特约摄影师名单&山西视觉志 2019-02-05
  • 广州旅游为何爆发出强大且持续的吸引力 2019-02-05
  • 今天看啥
      热点:

        北京赛车pk10历史记录 www.zwahn.com

        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

        北京赛车pk10历史记录 www.zwahn.comtrue//www.zwahn.com/IOSjc/1316414.htmlTechArticleiOS, 在实际开发中,协议的应用非常广泛,以下是实际应用的例子。 1、协议的定义: myProtocolDelegate.h // // myProtocolDelegate.h // zlwPlayerAppli...

        相关文章

          暂无相关文章
        相关搜索:

        帮客评论

        视觉看点
      • 10月26日十九大新闻发言人专题新闻发布会 2019-03-22
      • 多交140分“亚裔税”?哈佛大学被控歧视亚裔学生 2019-03-22
      • 男子农田挖到古董抠出吓得魂飞魄散 2019-03-17
      • 瑞典南部城市发生枪击案致5人伤 警方:与恐袭无关 2019-03-10
      • 西安幼升小入学难问题出在哪儿教育部门将统筹协调安排 2019-03-10
      • 传统强队集体慢热(世界杯纵横) 2019-03-10
      • 乌鲁木齐水磨沟区开建两座立体停车库 2019-03-10
      • 山西新闻网特约摄影师名单&山西视觉志 2019-02-05
      • 广州旅游为何爆发出强大且持续的吸引力 2019-02-05