iOS - 协议实现的例子
程序员文章站
2022-03-30 12:56:14
在实际开发中,协议的应用非常广泛,以下是实际应用的例子。 1、协议的定义: myProtocolDelegate.h myProtocolDelegate.m 2、协议的调用和实现: ViewController.h ViewController.m ......
在实际开发中,协议的应用非常广泛,以下是实际应用的例子。
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
上一篇: Asp.net Mvc身份验证
下一篇: TypeEncodings
推荐阅读
-
ASP.net(c#)用类的思想实现插入数据到ACCESS例子
-
Python Django实现layui风格+django分页功能的例子
-
iOS开发中实现新闻图片的无限循环展示的方法
-
详解iOS开发中UItableview控件的数据刷新功能的实现
-
vue实现与安卓、IOS交互的方法
-
apache中使用mod_gnutls模块实现多个SSL站点配置(多个HTTPS协议的虚拟主机)
-
Python实现HTTP协议下的文件下载方法总结
-
iOS应用中UITableView左滑自定义选项及批量删除的实现
-
python实现DNS正向查询、反向查询的例子
-
iOS中使用schema协议调用APP和使用iframe打开APP的例子分析