当前位置: 首页 > news >正文

沈阳网站开发集团python在线编程器

沈阳网站开发集团,python在线编程器,seo软文代写,湘潭网站建设 要上磐石网络协议(Protocol)的作用#xff1a; 1. 规范接口#xff0c;用来定义一套公用的接口#xff1b; 2. 约束或筛选对象。 代理(Delegate)#xff1a; 它本身是一种设计模式#xff0c;委托一个对象遵守协议去做某件事情#xff0c;目的是为了降低对象间的耦合度#…协议(Protocol)的作用 1. 规范接口用来定义一套公用的接口 2. 约束或筛选对象。   代理(Delegate) 它本身是一种设计模式委托一个对象遵守协议去做某件事情目的是为了降低对象间的耦合度或用来逆向传值。   一、定义一套公用接口 1 /** 协议 */2 protocol ExecuteProtocol NSObject3 4 required5 /**6 * brief 必须实现的某个方法7 */8 - (NSUInteger)qualified;9 10 11 optional 12 /** 13 * brief 可选的方法 14 */ 15 - (void)doSomething; 16 17 end 协议只有.h文件没有.m文件。因为 Protocol 仅定义公用的一套接口并不能提供具体的实现方法。(具体的实现需要某个遵守了协议的类去实现然后该类就可以作为被筛选出来的对象做些事情后面会讲到)   假如控制器里面需要用到协议那么导入协议  1 #import ExecuteProtocol.h  并且实现协议的 required 方法(否则编译器会warning)   ViewController的代码如下 1 #import ViewController.h2 #import ExecuteProtocol.h3 #import Object.h4 5 interface ViewController ()6 property (nonatomic, strong) UILabel *label;7 end8 9 implementation ViewController 10 11 #pragma mark - View lifeCycle 12 - (void)viewDidLoad { 13 [super viewDidLoad]; 14 [self.view addSubview:self.label]; 15 [self getHouse:[[Object alloc] init]]; 16 } 17 18 - (void)getHouse:(id ExecuteProtocol)obj { 19 self.label.text [NSString stringWithFormat:%lu, [obj qualified]]; 20 } 21 22 #pragma mark - getter Methods 23 - (UILabel *)label { 24 if (!_label) { 25 _label [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; 26 _label.textAlignment NSTextAlignmentCenter; 27 _label.backgroundColor [UIColor redColor]; 28 } 29 return _label; 30 } 31 end 在控制器里面添加一个方法这个方法的参数必须是遵守了协议的某个对象所以创建了Object对象 1 #import Foundation/Foundation.h 2 #import ExecuteProtocol.h 3 4 /** 某对象 */ 5 interface Object : NSObject ExecuteProtocol 6 7 end 并且实现协议方法 1 #import Object.h 2 3 implementation Object 4 5 - (NSUInteger)qualified { 6 return 88; 7 } 8 9 end 简单的小Demo。   二、代理传值(SecondaryViewController 传值到 ViewController中) 1.在ViewController中 1 // ViewController需要 遵守代理 2 interface ViewController () SecondaryViewControllerDelegate 3 4 SecondaryViewController *secVC [[SecondaryViewController alloc] init]; 5 // 指定代理 6 secVC.delegate self; 7 [self.navigationController pushViewController:secVC animated:YES]; 1 // 实现required代理方法实现传值打印结果 2 #pragma mark - SecondaryViewControllerDelegate Methods 3 - (void)controller:(SecondaryViewController *)controller text:(NSString *)text { 4 NSLog(% %, controller, text); 5 }   2.在SecondaryViewController中 1)首先声明代理 1 #import UIKit/UIKit.h2 class SecondaryViewController;3 4 /**5 * brief 协议方法(类名Delegate)6 */7 protocol SecondaryViewControllerDelegate NSObject8 required9 /** 10 * brief 传值 11 * 12 * param controller 当前控制器 13 * param text 文本值 14 */ 15 - (void)controller:(SecondaryViewController *)controller text:(NSString *)text; 16 end 17 18 interface SecondaryViewController : UIViewController 19 /** 20 * brief 代理用weak修饰(防止内存泄露) 21 */ 22 property (nonatomic, weak) id SecondaryViewControllerDelegate delegate; 23 end 2)判断代理存在与否和方法是否响应 1 /** 2 * SecondaryViewController3 */4 - (void)touchesBegan:(NSSetUITouch * *)touches withEvent:(UIEvent *)event {5 /**6 * brief 判断是否设置了代理并且代理是否响应了代理方法7 */8 if (self.delegate [self.delegate respondsToSelector:selector(controller:text:)]) {9 [self.delegate controller:self text:传值]; 10 } 11 [self.navigationController popViewControllerAnimated:YES]; 12 } 源码戳这里 尊重作者劳动成果转载请注明 【kingdev】转载于:https://www.cnblogs.com/xiu619544553/p/5295079.html
http://www.zqtcl.cn/news/21924/

相关文章:

  • 轻松筹 的网站价格做公司网站建设ppt
  • 快速排名网站黑龙江住房和城乡建设厅网站
  • 福永网站制作台州百度搜索优化
  • 论坛网站推广苏州工程网站建设
  • 企业建站多少钱一个月买房子
  • 建设一个门户网站 费用网站怎么做分类聚合
  • 怎么用网站开发者工具更换网页wordpress点评
  • 手机网站主页设计俄罗斯局势最新消息
  • 深圳企业建设网站长沙市网站制作哪家专业
  • 移动互联网网站开发技术网站开发一般学多久
  • app网站开发者WordPress微信支付个人
  • 网站开发的目的意义特色创新四川不能去的设计院
  • 工作优化方案怎么写网站色哦优化8888
  • 泰州网站制作哪家好网站搜索框如何做
  • 哈尔滨大型网站制作石家庄做网站排名公司哪家好
  • 个人承接网站开发宣城网站seo
  • 服装网站策划设计哪个网站可以做公众号封面
  • 如何建设国外网站自己如何开发一个app
  • 宁德市建设局网站广州市政务中心官网
  • 做漫画的网站有哪些房屋室内设计
  • 从化低价网站建设长春火车站建在哪里
  • 网站推广网站关键词排名怎么做网站做好怎么推广
  • 网站目录文件查看有免费的服务器吗
  • 那个公司做的外贸网站好wordpress营业按钮
  • 网站建设服务图片html模板怎么用
  • 黄页88网全自动录播系统郑州网站优化公司价位
  • 微信的网站wordpress图片小程序
  • 现在建网站赚钱吗建设工程合同在性质上属于什么合同
  • 阿里云带宽5m能做什么网站石家庄网站制作工具
  • 网站建设如何吸引投资wordpress建站案例