减少UIViewController切换的耦合
我们一般切换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切换的耦合的更多相关文章
- Effective前端5:减少前端代码耦合
什么是代码耦合?代码耦合的表现是改了一点毛发而牵动了全身,或者是想要改点东西,需要在一堆代码里面找半天.由于前端需要组织js/css/html,耦合的问题可能会更加明显,下面按照耦合的情况分别说明: ...
- iOS开发系列--视图切换
概述 在iOS开发中视图的切换是很频繁的,独立的视图应用在实际开发过程中并不常见,除非你的应用足够简单.在iOS开发中常用的视图切换有三种,今天我们将一一介绍: UITabBarController ...
- iOS学习笔记04-视图切换
一.视图切换 UITabBarController (分页控制器) - 平行管理视图 UINavigationController (导航控制器) - 压栈出栈管理视图 模态窗口 二.UITabBar ...
- iOS 自己定义页面的切换动画与交互动画 By Swift
在iOS7之前,开发人员为了寻求自己定义Navigation Controller的Push/Pop动画,仅仅能受限于子类化一个UINavigationController,或是用自己定义的动画去覆盖 ...
- 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. ...
- Github上关于iOS的各种开源项目集合(强烈建议大家收藏,查看,总有一款你需要)
下拉刷新 EGOTableViewPullRefresh - 最早的下拉刷新控件. SVPullToRefresh - 下拉刷新控件. MJRefresh - 仅需一行代码就可以为UITableVie ...
- iOS及Mac开源项目和学习资料【超级全面】
UI 下拉刷新 EGOTableViewPullRefresh – 最早的下拉刷新控件. SVPullToRefresh – 下拉刷新控件. MJRefresh – 仅需一行代码就可以为UITable ...
- iOS:iOS开发非常全的三方库、插件等等
iOS开发非常全的三方库.插件等等 github排名:https://github.com/trending, github搜索:https://github.com/search. 此文章转自git ...
- iOS开发--iOS及Mac开源项目和学习资料
文/零距离仰望星空(简书作者)原文链接:http://www.jianshu.com/p/f6cdbc8192ba著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 原文出处:codecl ...
随机推荐
- 数字游戏(string的sort的应用)
题目描述 牛牛举办了一场数字游戏,有n个玩家参加这个游戏,游戏开始每个玩家选定一个数,然后将这个数写在纸上(十进制数,无前缀零),然后接下来对于每一个数字将其数位按照非递减顺序排列,得到新的数,新数的 ...
- UVA-1602 Lattice Animals 搜索问题(打表+set)
题目链接 https://vjudge.net/problem/UVA-1602 紫书的一道例题,跟之前的很多题目有很多不同. 本题不像是一般的dfs或bfs这样的搜索套路,而是另一种枚举思路. 题意 ...
- 紫书 习题 8-22 UVa 1622 (构造法)
这道题的构造法真的复杂--要推一堆公式--这道题写了几天了--还是没写出来-- 一开始简单的觉得先左右来回, 然后上下来回, 然后把剩下的执行完了好了, 然后就WA. 然后换了个思路, 觉得是贪心, ...
- Java中四种复制数组的方法
JAVA语言的下面几种数组复制方法中,哪个效率最高? B.效率:System.arraycopy > clone > Arrays.copyOf > for循环 1.System.a ...
- Qt之OpenSSL
简述 OpenSSL是一个强大的安全套接字层密码库,囊括主要的密码算法.常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用. 简述 下载安装 使用 更多参考 下载安装 ...
- Mybatis Generator for SQL Server
Mybatis Generator for SQL Server <?xml version="1.0" encoding="UTF-8" ?> & ...
- tomcatserver管理界面username和password忘记
tomcatserverhttp://localhost:8080/ 这样訪问,点击Manager App后要求输入username和password才干进入管理应用界面 我忘记了username和p ...
- 小贝_mysql select5种子句介绍
mysql select5种子句介绍 简要 一.五种字句 二.具体解释五种字句 一.五种字句 where.group by.having.order by.limit 二.具体解释五种字句 2.1.理 ...
- BootStrap有用代码片段(持续总结)
> 如题.持续总结自己在使用BootStrap中遇到的问题.并记录解决方法.希望能帮到须要的小伙伴 1.bootstrap上下布局.顶部固定下部填充 应用场景:经典上下布局中,顶部导航条固定,下 ...
- js绑定事件方法:addEventListener与attachEvent的不同浏览器的兼容性写法
js的事件绑定方法中,ie仅仅支持attachEvent,而FF和Chrome仅仅支持addEventListener,所以就必须为这两个方法做兼容处理,原理是先推断attachEvent仅仅否为真( ...