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

  1. #import "UIViewControllerDemo.h"
  2.  
  3. UIViewControllerDemo *vc = [UIViewControllerDemo alloc] initWithNibName:nil bundle:nil] autorelease];
  4. [self.navigationController pushViewController:vc animated:YES];

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

  1. #import "UIViewControllerDemo1.h"
  2. #import "UIViewControllerDemo2.h"
  3. #import "UIViewControllerDemo3.h"
  4. #import "UIViewControllerDemo4.h"
  5.  
  6. UIViewControllerDemo1 *vc1
  7. UIViewControllerDemo2 *vc2
  8. UIViewControllerDemo3 *vc3
  9. UIViewControllerDemo4 *vc4

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

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

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

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

  1. @protocol XYSwitchControllerProtocol <NSObject>
  2. -(id) initWithObject:(id)object;
  3. @end
  4.  
  5. -(void) pushVC:(NSString *)vcName object:(id)object{
  6. Class class = NSClassFromString(vcName);
  7. NSAssert(class != nil, @"Class 必须存在");
  8. UIViewController *vc = nil;
  9.  
  10. if ([class conformsToProtocol:@protocol(XYSwitchControllerProtocol)]) {
  11. vc = [[[NSClassFromString(vcName) alloc] initWithObject:object] autorelease];
  12. }else {
  13. vc = [[[NSClassFromString(vcName) alloc] init] autorelease];
  14. vc.parameters = object;
  15. }
  16.  
  17. [self.navigationController pushViewController:vc animated:YES];
  18. }

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

  1. -(void) modalVC:(NSString *)vcName withNavigationVC:(NSString *)nvcName object:(id)object succeed:(UIViewController_block_void)block{
  2. Class class = NSClassFromString(vcName);
  3. NSAssert(class != nil, @"Class 必须存在");
  4.  
  5. UIViewController *vc = nil;
  6.  
  7. if ([class conformsToProtocol:@protocol(XYSwitchControllerProtocol)]) {
  8. vc = [[[NSClassFromString(vcName) alloc] initWithObject:object] autorelease];
  9. }else {
  10. vc = [[[NSClassFromString(vcName) alloc] init] autorelease];
  11. vc.parameters = object;
  12. }
  13.  
  14. UINavigationController *nvc = nil;
  15. if (nvcName) {
  16. nvc = [[[NSClassFromString(vcName) alloc] initWithRootViewController:vc] autorelease];
  17. [self presentViewController:nvc animated:YES completion:block];
  18.  
  19. return;
  20. }
  21.  
  22. [self presentViewController:vc animated:YES completion:block];
  23. }

减少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. mongodb报错:connection refused because too many open connections: 819

    问题: 发现mongodb无法连接,查看mongodb日志,出现大量的如下报错: [initandlisten] connection refused because too many open co ...

  2. 获取鼠标经过处的标签的标签名和id

    <script> var el = window.document.body; // 声明一个变量,默认值为body window.document.body.onmouseover = ...

  3. 将对象a的属性赋值给对象b

    BeanUtils.copyProperties(a,b); 将a的属性赋值给b(ab的共同属性)

  4. 修改maven打包名字

    仅需在pom.xml添加下列配置 build> <finalName>userapi</finalName> </build>

  5. HDU-4296 Buildings 贪心 从相邻元素的相对位置开始考虑

    题目链接:https://cn.vjudge.net/problem/HDU-4296 题意 有很多板子,每一个板子有重量(w)和承重(s)能力 现规定一块板子的PDV值为其上所有板子的重量和减去这个 ...

  6. 2015 Multi-University Training Contest 2 hdu 5308 I Wanna Become A 24-Point Master

    I Wanna Become A 24-Point Master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 ...

  7. 在oracle中采用connect by prior来实现递归查询

    注明:该文章为引用别人的文章,链接为:http://blog.csdn.net/apicescn/article/details/1510922 , 记录下来只是为了方便查看 原文: connect ...

  8. POJ——T2553 The Bottom of a Graph

    http://poj.org/problem?id=2553 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10987   ...

  9. [Javascript] Required function arguments in Javascript

    In Javascript, all function arguments are optional by default. That means if you ever forget to pass ...

  10. vue2.0 路由学习笔记

    昨天温故了一下vue2.0的路由 做个笔记简单记录一下! 1.首相和vue1.0一样 要使用vuejs的路由功能需要先引入vue-router.js 2.然后修改原有a标签处代码 这里以一个ul li ...