【iOS】Class对构造简洁代码很有帮助
来源:互联网 发布:网络十大热词 英文 编辑:程序博客网 时间:2024/06/05 18:47
(这到底取的是什么标题啊)
首先先看这段代码(有删减)
@property (nonatomic, copy)NSMutableArray <NSMutableArray *>*datas;- (void)viewDidLoad { NSMutableArray *section0 = @[ @{@"title" : @"我的借阅", @"leftIcon" : @"my_borrow", @"vc" : @"ManageBooksViewController"}.mutableCopy, @{@"title" : @"我的书籍", @"leftIcon" : @"my_book", @"vc" : @"MyFileViewController"}.mutableCopy, ].mutableCopy ... self.datas = @[section0,section1,section2, section3].mutableCopy;}#pragma mark 选择事件- (void) tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSInteger row = indexPath.row; NSInteger section = indexPath.section; NSString *vcStr = self.datas[section][row][@"vc"]; if ([vcStr isEqualToString:@"MineIntroductionViewController"]) { ActivityWebViewController *vc = [[ActivityWebViewController alloc]initWithContentUrl:@"http://librarymanager.30days-tech.com/h5/introduce.html" title:@"使用说明" presenting:NO]; vc.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:vc animated:YES]; } if ([vcStr isEqualToString:@"MyFileViewController"]) { ManageBooksViewController *vc = [[ManageBooksViewController alloc]init]; vc.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:vc animated:YES]; } else if ([vcStr isEqualToString:@"MyBooksViewController"]) { MyFileViewController *vc = [[MyFileViewController alloc]init]; vc.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:vc animated:YES]; } else if ([vcStr isEqualToString:@"MyMoreSettingViewController"]) { MyMoreSettingViewController *vc = [[MyMoreSettingViewController alloc]init]; vc.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:vc animated:YES]; } .......}
着看之下貌似没什么问题,语法也说得过去,而且总比用indexPath来判断进入哪个控制器要简单得多,后期修改也不存在太大的问题,但随着需求的增加我们会发现每多出一个控制器,push控制器代码又会多出一段:
XXX *vc = [[XXX alloc]init]; vc.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:vc animated:YES];
so,利用Class就可以瞬间把代码缩减好了。
#pragma mark 选择事件- (void) tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSInteger row = indexPath.row; NSInteger section = indexPath.section; NSString *vcStr = self.datas[section][row][@"vc"]; Class vcClass = NSClassFromString(vcStr) ; if (vcClass) { UIViewController *vc = [[vcClass alloc]init]; vc.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:vc animated:YES]; return ; }}
看,很方便吧!
阅读全文
0 0
- 【iOS】Class对构造简洁代码很有帮助
- 对IOS新手开发有帮助的一些小代码
- AngularJS风格指南,对代码规范很有帮助
- 感觉这个对生活很有帮助!
- C++小程序 对理解析构和构造函数有帮助(我认为 哈哈~)
- [转]对职业规划很有帮助的帖子
- 正则表达式基础(对初学者很有帮助)
- 手上健身球对练习木吉他很有帮助
- 一个好结构对演讲很有帮助
- 【茶对人的健康很有帮助】
- 对计划搞安卓移动端的很有帮助
- 我是一个线程(对理解多线程很有帮助)
- 我是一个线程(对理解多线程很有帮助)
- 有帮助的文章,代码!
- 搜索引擎会对博客很有好吗? 会对网站有帮助吗
- 最好自己手动敲下代码,对理解有很大帮助。
- GCC扩展(转)–对看KERNEL代码有帮助
- matlab 常用代码(有数据,可以运行,对科研帮助大)案例以后补齐
- Android 判断手机是否root
- POJ 1651 Language: Multiplication Puzzle
- 基于Dragonboard 410c的智能小车(四)
- Mysql按分组获取前几条内容
- Android | 日志记录的级别与方法
- 【iOS】Class对构造简洁代码很有帮助
- 第三章运算符、表达式和语句
- 有人问我:程序员要供祖师爷的话该供谁?
- 区块链?人工智能?2018 年你应该了解的十大技术趋势
- 2017年11月编程语言排行榜:Java持续下滑仍稳居第1,Python逆袭C#上升到第4
- 数据库基础 01
- React安装 环境搭建
- Js闭包
- String-StringBuffer与StringBuilder之间区别