我们一般切换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. 第三方库requests

    requests库 # 1.记得安装 第三方 模块 requests # pip install requests import requests url = 'http://www.baidu.co ...

  2. JAVA调用接口

    HttpUrlconnection部分 //发送JSON字符串 如果成功则返回成功标识. public static String doJsonPost(String urlPath, String ...

  3. BZOJ 2560(子集DP+容斥原理)

    2560: 串珠子 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 757  Solved: 497[Submit][Status][Discuss] ...

  4. AWS中国EC2 公网IP登录免pemKEY修改shh 配置文件

    个人使用记录 1:KEY 授权 chmod 400 VPN.pem 2:连接 ssh -i "VPN.pem" ubuntu@ec2-54-183-119-93.us-west-1 ...

  5. exe文件作为服务启动

    一. 准备软件 instsrv.exe srvany.exe 这两个都是 Microsoft Windows Resource Kits 里面的小工具 链接:http://pan.baidu.com/ ...

  6. 通过force index了解的MySQL查询的性能优化

    查询是数据库技术中最常用的操作.查询操作的过程比较简单,首先从客户端发出查询的SQL语句,数据库服务端在接收到由客户端发来的SQL语句后, 执行这条SQL语句,然后将查询到的结果返回给客户端.虽然过程 ...

  7. 使用JMX透过防火墙远程监控tomcat服务

    https://my.oschina.net/mye/blog/64879 http://blog.csdn.net/l1028386804/article/details/51547408 http ...

  8. 4.有关日期格式属性改动常识,v$nls_parameters,between and,查询指定部门的员工信息,in和null,like模糊查询,order by后面能够跟:列名、表达式、别名、序号

     1 有关日期格式属性改动常识 NLS_DATE_FORMAT           DD-MON-RR select sysdate from dual; NLS_CURRENCY         ...

  9. less06 引入(importing)

    main.less @wp:960px; .colorsss{ color: darkgreen; } index.css .color{ color: #ff6600; } style.less / ...

  10. .Net配置虚拟域名

    1.在IIS中配置和地址端口,和名称. 2.在hosts文件中加上地址匹配. 3.重启IIS管理网站. 就可以通过虚拟域名进行访问了.