zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

iOS设计模式之代理模式

2023-09-14 08:57:54 时间
远程代理:就是为一个对象在不同的地址空间提供据不代表。这样可以隐藏一个对象存在于不同地址空间的事实。 虚拟代理:是根据需要创建开销很大的对象,通过它来存放实例化需要很长时间的真实对象。 安全代理:用来控制真实对象访问时的权限。
*智能指引:是指当调用真实的对象时,代理处理另外一些事。

ChildViewCongroller.h

// // ChildViewController.h // DelegateDemo // // Created by zhanggui on 15/8/6. // Copyright (c) 2015年 zhanggui. All rights reserved. // #import UIKit/UIKit.h @protocol ChildDelegate NSObject 

-(void)changeColor:(UIColor *)color;

@end @interface ChildViewController : UIViewController

@property(assign,nonatomic)id ChildDelegate ChildDelegate;

@end

ChildVIewController.m

// // ChildViewController.m // DelegateDemo // // Created by zhanggui on 15/8/6. // Copyright (c) 2015年 zhanggui. All rights reserved. // #import "ChildViewController.h" @interface ChildViewController () @end @implementation ChildViewController

- (void)viewDidLoad {

 [super viewDidLoad];

 self.view.backgroundColor = [UIColor whiteColor];

 UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, 200, 50)];

 [button addTarget:self action:@selector(show) forControlEvents:UIControlEventTouchUpInside];

// button.backgroundColor = [UIColor redColor];

 [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

 [button setTitle:@"返回调用代理" forState:UIControlStateNormal];

 [self.view addSubview:button];

-(void)show {

 [_ChildDelegate changeColor:[UIColor redColor]];

 [self.navigationController popToRootViewControllerAnimated:YES];

@end

在一个ViewController中去push出来ChildViewController。点击ChildViewController中的按钮改变根视图的背景色
ViewController.h

// // ViewController.h // DelegateDemo // // Created by zhanggui on 15/8/6. // Copyright (c) 2015年 zhanggui. All rights reserved. // #import UIKit/UIKit.h #import "ChildViewController.h" @interface ViewController : UIViewController ChildDelegate 


淘宝iOS扫一扫架构升级 - 设计模式的应用 本文在“扫一扫功能的不断迭代,基于设计模式的基本原则,逐步采用设计模式思想进行代码和架构优化”的背景下,对设计模式在扫一扫中新的应用进行了总结。
1.外观模式简介 外观模式(Facade)在开发过程中的运用频率非常高,尤其是在现阶段各种第三方SDK充斥在我们的周边,而这些SDK很大概率会使用外观模式。
模型-视图-控制器(Model-View-Controller,MVC)是Xerox PARC在20世纪80年代为编程语言Smalltalk-80发明的一种软件设计模式,至今已广泛应用于用户交互应用程序中。
建议45:设计模式是特定环境下的特定问题的解决方案 设计模式是某种特定设计的模板或指导原则。 建议46:MVC模式是一种复合或聚合模式 MVC 是一种高级别的模式,关注的是应用程序的全局架构,并根据各种对象在程序中发挥的作用对其进行分类。