我们一般切换UIViewController的时候用的是例如以下代码

#import "UIViewControllerDemo.h"

UIViewControllerDemo *vc = [UIViewControllerDemo alloc] initWithNibName:nil bundle:nil] autorelease];
[self.navigationController pushViewController:vc animated:YES];

当我们须要切换的UIViewController多的时候呢,代码难免写成:

#import "UIViewControllerDemo1.h"
#import "UIViewControllerDemo2.h"
#import "UIViewControllerDemo3.h"
#import "UIViewControllerDemo4.h" UIViewControllerDemo1 *vc1
UIViewControllerDemo2 *vc2
UIViewControllerDemo3 *vc3
UIViewControllerDemo4 *vc4

以后维护的时候非常是麻烦

以下我们将对UIViewController切换的方法进行改造

-(void) pushVC:(NSString *)vcName{
Class class = NSClassFromString(vcName);
NSAssert(class != nil, @"Class 必须存在");
UIViewController *vc = [[[NSClassFromString(vcName) alloc] init] autorelease]; [self.navigationController pushViewController:vc animated:YES];
}

这里能够增加一个协议,使用我们指定的方法来初始化,这样能够在初始话的同一时候带入參数

@protocol XYSwitchControllerProtocol <NSObject>
-(id) initWithObject:(id)object;
@end -(void) pushVC:(NSString *)vcName object:(id)object{
Class class = NSClassFromString(vcName);
NSAssert(class != nil, @"Class 必须存在");
UIViewController *vc = nil; if ([class conformsToProtocol:@protocol(XYSwitchControllerProtocol)]) {
vc = [[[NSClassFromString(vcName) alloc] initWithObject:object] autorelease];
}else {
vc = [[[NSClassFromString(vcName) alloc] init] autorelease];
vc.parameters = object;
} [self.navigationController pushViewController:vc animated:YES];
}

模态的方法则多个考虑个UINavigationController

-(void) modalVC:(NSString *)vcName withNavigationVC:(NSString *)nvcName object:(id)object succeed:(UIViewController_block_void)block{
Class class = NSClassFromString(vcName);
NSAssert(class != nil, @"Class 必须存在"); UIViewController *vc = nil; if ([class conformsToProtocol:@protocol(XYSwitchControllerProtocol)]) {
vc = [[[NSClassFromString(vcName) alloc] initWithObject:object] autorelease];
}else {
vc = [[[NSClassFromString(vcName) alloc] init] autorelease];
vc.parameters = object;
} UINavigationController *nvc = nil;
if (nvcName) {
nvc = [[[NSClassFromString(vcName) alloc] initWithRootViewController:vc] autorelease];
[self presentViewController:nvc animated:YES completion:block]; return;
} [self presentViewController:vc animated:YES completion:block];
}

减少UIViewController切换的耦合的更多相关文章

  1. Effective前端5:减少前端代码耦合

    什么是代码耦合?代码耦合的表现是改了一点毛发而牵动了全身,或者是想要改点东西,需要在一堆代码里面找半天.由于前端需要组织js/css/html,耦合的问题可能会更加明显,下面按照耦合的情况分别说明: ...

  2. iOS开发系列--视图切换

    概述 在iOS开发中视图的切换是很频繁的,独立的视图应用在实际开发过程中并不常见,除非你的应用足够简单.在iOS开发中常用的视图切换有三种,今天我们将一一介绍: UITabBarController ...

  3. iOS学习笔记04-视图切换

    一.视图切换 UITabBarController (分页控制器) - 平行管理视图 UINavigationController (导航控制器) - 压栈出栈管理视图 模态窗口 二.UITabBar ...

  4. iOS 自己定义页面的切换动画与交互动画 By Swift

    在iOS7之前,开发人员为了寻求自己定义Navigation Controller的Push/Pop动画,仅仅能受限于子类化一个UINavigationController,或是用自己定义的动画去覆盖 ...

  5. ios项目里扒出来的json文件

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0. ...

  6. Github上关于iOS的各种开源项目集合(强烈建议大家收藏,查看,总有一款你需要)

    下拉刷新 EGOTableViewPullRefresh - 最早的下拉刷新控件. SVPullToRefresh - 下拉刷新控件. MJRefresh - 仅需一行代码就可以为UITableVie ...

  7. iOS及Mac开源项目和学习资料【超级全面】

    UI 下拉刷新 EGOTableViewPullRefresh – 最早的下拉刷新控件. SVPullToRefresh – 下拉刷新控件. MJRefresh – 仅需一行代码就可以为UITable ...

  8. iOS:iOS开发非常全的三方库、插件等等

    iOS开发非常全的三方库.插件等等 github排名:https://github.com/trending, github搜索:https://github.com/search. 此文章转自git ...

  9. iOS开发--iOS及Mac开源项目和学习资料

    文/零距离仰望星空(简书作者)原文链接:http://www.jianshu.com/p/f6cdbc8192ba著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 原文出处:codecl ...

随机推荐

  1. SAI / PS绘画一个卡通女孩详解

    本教程介绍使用SAI / PS绘画一个卡通女孩的教程 ,教程很详细,动起你的小手一起来试试吧! 软件下载:http://www.dongmansoft.com/xiazai.html 想要Get到更多 ...

  2. React中的AES加解密请求

    引言 在我们使用React开发Web前端的时候,如果是比较大的项目和正常的项目的话,我们必然会用到加解密,之前的文章中提到.NET的一些加解密,那么,这里我就模拟一个例子: 1.后台开发API接口,但 ...

  3. [agc008d]kth-k

    题意: 给你一个长度为N的整数序列X,构造一个整数序列a满足: 1.a的长度为$N^2$,且1~N中每个数字恰好出现N次: 2.数字i在a中第i次出现的位置为$X_i$: 如果不能构造输出“No”,否 ...

  4. wepy框架的API的预加载$preload这功能阔以喔

    优势:比 url 传递.或是 storage .或是 globalData 更方便 1:如 url 不能直接传一个 Object 要传的又要序列化与反序列化操作,麻烦(普通的单个变量还是挺便捷简单实在 ...

  5. vue-cli#2.0项目结构分析

    项目结构 build 构建工具相关的目录 config 配置目录 dist 通过工具打包生成的最终需要上线的目录 node_modules 存放本地开发所有的依赖包的目录 src 源码目录 stati ...

  6. 【转载】python学习之 字符串前'r'的用法

    文章转载:https://www.cnblogs.com/cyiner/archive/2011/09/18/2180729.html 在打开文件的时候open(r'c:\....') 加r和不加'' ...

  7. Oracle with as 嵌套

    oracle  with as可以理解为临时视图,可以极大的简化sql语句,并且支持嵌套使用. With c3 As(Select * From v_tr_daily Where p_date=to_ ...

  8. 洛谷 P3914 染色计数

    P3914 染色计数 题目描述 有一颗NN个节点的树,节点用1,2,\cdots,N1,2,⋯,N编号.你要给它染色,使得相邻节点的颜色不同.有MM种颜色,用1,2,\cdots,M1,2,⋯,M编号 ...

  9. 转:iPhone libxml2 not found during build

    在新建的一个项目中,出现编译错误,发现是缺少了libxml2.dylib,后面将这个资源包添加了,编译还是出现标题上所说的问题 #import <libxml/tree.h> //#imp ...

  10. nyoj-673-悟空的难题(数组标记)

    悟空的难题 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描写叙述 自从悟空当上了齐天大圣.花果山上的猴子猴孙们便也能够尝到天上的各种仙果神酒,所以猴子猴孙们的体质也得到了非 ...