降低UIViewController切换的耦合
来源:互联网 发布:python ascii转字符 编辑:程序博客网 时间:2024/05/16 11:54
降低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];}
0 0
- 降低UIViewController切换的耦合
- 降低UIViewController切换的耦合
- 降低UIViewController切换的耦合
- 如何降低代码的耦合
- 降低耦合。。。
- 降低耦合
- 降低耦合
- 降低耦合
- 如何降低类之间的耦合
- 如何降低一个程序的耦合性
- 如何降低一个程序的耦合性
- 降低程序耦合性的方法
- 软件耦合的度量和降低
- 降低对象之间的耦合关系
- UIViewController的切换方式
- UIViewController的切换效果
- 降低耦合严重度
- UIViewController 之间的动画切换
- 数组的逆序输出
- Eigen初步1:初步体验Eigen库
- TYVJ 最美妙的矩阵
- How to use marks in Vim
- js实现通用的微信分享组件示例
- 降低UIViewController切换的耦合
- 查找占用磁盘最多的10个文件夹
- PostMessage与PostThreadMessage的区别
- win7下显示系统的后缀名
- ios--UIActionSheet的使用
- register_chrdev()与unregister_chrdev()
- 页面中文字不被选中
- 单例模式
- 线程间如何进行消息传递