我们一般切换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. SpringCloud学习笔记(15)----Spring Cloud Netflix之Hystrix Dashboard的使用

    1. 引入依赖 在前面几节中的消费者中添加pom依赖. <dependency> <groupId>org.springframework.cloud</groupId& ...

  2. DNS BIND之dnssec安全介绍

    Domain Name System Security Extensions (DNSSEC)DNS安全扩展,是由IETF提供的一系列DNS安全认证的机制(可参考RFC2535).它提供了一种来源鉴定 ...

  3. 洛谷P3355 骑士共存问题 二分图_网络流

    Code: #include<cstdio> #include<cstring> #include<queue> #include<vector> #i ...

  4. HDU-5685 Problem A 求乘法逆元

    题目链接:https://cn.vjudge.net/problem/HDU-5685 题意 给一个字符串S和一个哈希算法 $ H(s)=\prod_{i=1}^{i\leq len(s)}(S_{i ...

  5. [HEOI2012]采花(树状数组+离线)

    听说这题的所发和HH的项链很像. 然而那道题我使用莫队写的... 这是一个套路,pre数组加升维(在线). 记录一个\(pre\)数组,\(pre[i]\)代表上一个和i颜色相同的下标. 我们把询问离 ...

  6. java webproject中logback换配置文件的路径

    本人小站点:   http://51kxd.com/  欢迎大家不开心的时候訪问訪问,调节一下心情 web.xml中配置: <!-- windows  logback.xml文件跟web容器(比 ...

  7. JNI之——&#39;cl&#39; 不是内部或外部命令,也不是可执行的程序或批处理文件

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46604315 问题的出现:     今天卸载了VS2010,重装vs2008后.发 ...

  8. backtrack5实现局域网DNS欺骗

    前言:不得不说Linux下的神器挺多,越来越喜欢Linux了.. . 測试环境            linux backtrack 5            windows xp 先在Linux下开 ...

  9. PHP join() 函数

    PHP join() 函数 实例 把数组元素组合为一个字符串: <?php $arr = array('Hello','World!','I','love','Shanghai!'); echo ...

  10. django uWSGI nginx搭建一个web服务器 确定可用

    网上的找了很多篇 不知道为什么不行,于是自己搭建了一个可用的Web 大家可按步骤尝试 总结下基于uwsgi+Nginx下django项目生产环境的部署 准备条件: .确保有一个能够用runserver ...