【iOS】Class对构造简洁代码很有帮助
(这到底取的是什么标题啊)
首先先看这段代码(有删减)
@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 *)tableView
didSelectRowAtIndexPath:(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 *)tableView
didSelectRowAtIndexPath:(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 ;
}
}
看,很方便吧!
【iOS】Class对构造简洁代码很有帮助的更多相关文章
- 一个leetcode解题报告类目,代码很简洁
http://bookshadow.com/leetcode/ 里面的代码很简洁.可以看.
- iOS开发数据库篇—SQL代码应用示例
iOS开发数据库篇—SQL代码应用示例 一.使用代码的方式批量添加(导入)数据到数据库中 1.执行SQL语句在数据库中添加一条信息 插入一条数据的sql语句: 点击run执行语句之后,刷新数据 2.在 ...
- iOS开发UI篇—从代码的逐步优化看MVC
iOS开发UI篇—从代码的逐步优化看MVC 一.要求 要求完成下面一个小的应用程序. 二.一步步对代码进行优化 注意:在开发过程中,优化的过程是一步一步进行的.(如果一个人要吃五个包子才能吃饱,那么他 ...
- 【iOS 使用github上传代码】详解
[iOS 使用github上传代码]详解 一.github创建新工程 二.直接添加文件 三.通过https 和 SSH 操作两种方式上传工程 3.1https 和 SSH 的区别: 3.1.1.前者可 ...
- 关于eclipse保存代码很慢,提示the user operation is waiting的问题
关于eclipse保存代码很慢,提示the user operation is waiting的问题 首先 去掉 project - build Automaticlly 然后 project-> ...
- IOS 推送-配置与代码编写
IOS 推送配置与代码编写 这里介绍IOS的推送,本文章已经在IOS6/7/8上都能运行OK,按照道理IOS9应该没问题. 大纲: 1.文章前提 2.推送介绍 3.推送文件账号设置 4.推送证书介绍 ...
- IOS证书/私钥/代码签名/描述文件
1. 相关资源 (1) 钥匙串程序(常用工具->钥匙串),用于创建证书请求.安装证书.导出私钥等 (2) IOS开发中心:https://developer.apple.com/de ...
- 李洪强iOS开发之后使用纯代码实现横向滚动的UIScrollView
李洪强iOS开发之后使用纯代码实现横向滚动的UIScrollView (VTmagic是一个实现左右滚动的控制器的框架,也可以实现此功能) 实现的效果: 01 - 创建四个控制器 02 - 定义需要 ...
- Unity3D开发之“获取IOS设备所在的国家代码"
原地址:http://dong2008hong.blog.163.com/blog/static/469688272014021025578/ 在前一段时间游戏开发中需要实现获取IOS设备所在的国家代 ...
- git pull 代码很慢的问题
办公环境调整,之前开发机是和自己的电脑放同一网段内的,现在开发机放至到本地其他网段内,造成pull 代码很慢的问题,在网上查了一下 以下是原文,链接为 http://blog.sina.com.cn/ ...
随机推荐
- [Go] Viper 加载项目配置,go build 打包配置文件进二进制
Viper 的传统用法局部,加载到某个 package 下的全局变量后,其它 package 可以继续使用. var Conf *viper.Viper func init() { // File n ...
- dotnet C# 通过 Vortice 使用 Direct2D 特效入门
本文将告诉大家如何通过 Vortice 使用 D2D 的特效 本文属于 DirectX 系列博客,更多 DirectX 和 D2D 以及 Vortice 库的博客,请参阅我的 博客导航 上一篇: Di ...
- 2018-4-15-WPF-在-Alt+Tab-隐藏窗口
title author date CreateTime categories WPF 在 Alt+Tab 隐藏窗口 lindexi 2018-04-15 10:13:40 +0800 2018-3- ...
- 火山引擎VeDI:如何高效使用A/B实验,优化APP推荐系统
更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群 在移动互联网飞速发展的时代,用户规模和网络信息量呈现出爆炸式增长,信息过载加大了用户选择的难度,这样的背景下,推荐 ...
- SQL中常用的字符串REPLACE函数和LEN函数详解!
首发微信公众号:SQL数据库运维 原文链接:https://mp.weixin.qq.com/s?__biz=MzI1NTQyNzg3MQ==&mid=2247485212&idx=1 ...
- 使用DP-Modeler、ModelFun模方软件修复实景三维模型教程
P-Modeler DP-Modeler是武汉天际航自主研发的一款集精细化单体建模与Mesh网格模型修饰于一体的软件.支持三维模型一键水面修复.道路置平.建筑局部修饰.删除底部碎片.植被处理.桥隧 ...
- uniapp关于uni.getUserProfile的使用
点击查看代码 <button @click="getMy" data-eventsync="true">获取信息</button> le ...
- [从零学习C++][01]如何在Clion中定义多个main函数
在一个Clion项目中定义多个main函数编译的时候会报这个错误 显示就是main函数重复了,查阅了下好像可以通过修改CMakeList.txt来修改,将其定义成两个独立的executable即可 a ...
- 若依报错:登录状态已过期,您可以继续留在该页面,或者重新登录;When allowCredentials is true, allowedOrigins cannot contain the special value "*" since that cannot be set on the "Access-Control-Allow-Origin" response header.
报错界面 后台报错 java.lang.IllegalArgumentException: When allowCredentials is true, allowedOrigins cannot c ...
- Python:用Pandas输出格式化HTML并高亮
输出格式化的HTML 我们已知一个DataFrame记录了模型Model1.Model2在3个Epoch优化中的精度变化情况: frame = pd.DataFrame({"Model1&q ...