【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/ ...
随机推荐
- CF916E 换根树上问题
Link 题意:对一棵树进行三种操作. 把根设为 \(x\). 将以 \(lca(y, z)\) 为根的子树中所有点的权值加 \(v\). 查询以 \(x\) 为根的子树点权之和. 初始根为 \(1\ ...
- golang cron定时任务简单实现
目录 星号(*) 斜线(/) 逗号(,) 连字符 (-) 问好 (?) 常用cron举例 使用说明 golang 实现定时服务很简单,只需要简单几步代码便可以完成,不需要配置繁琐的服务器,直接在代码中 ...
- fastposter v2.8.0 发布 电商海报编辑器
fastposter v2.8.0 发布 电商海报编辑器 fastposter海报生成器,电商海报编辑器,电商海报设计器,fast快速生成海报 海报制作 海报开发.二维码海报,图片海报,分享海报,二维 ...
- python教程5:函数编程
函数编程 特性: 1.减少重复代码 2.让程序变的可扩展 3.使程序变得易维护 定义: 默认参数 要求:默认参数放在其他参数右侧 指定参数(调用的时候) 正常情况下,给函数传参数要按顺序,如果不想按 ...
- UploadLabs靶场
目录 Pass-01 Pass-02 Pass-03 Pass-04 Pass-05 Pass-06 Pass-07 Pass-08 Pass-09 Pass-10 Pass-11 Pass-12 P ...
- 热更学习笔记10~11----lua调用C#中的List和Dictionary、拓展类中的方法
[10]Lua脚本调用C#中的List和Dictionary 调用还是在上文中使用的C#脚本中Student类: lua脚本: print("------------访问使用C#脚本中的Li ...
- 简单 python 爬虫(爬取小说网)
#https://blog.csdn.net/Isana_Yashiro/article/details/84582946 import requestsfrom bs4 import Beautif ...
- Django项目目录结构
- Vue cli构建项目
一.创建项目 vue create hello-world 你会被提示选取一个 preset.你可以选默认的包含了基本的 Babel + ESLint 设置的 preset,也可以选"手动选 ...
- rofi编译支持中文输入版本
准备工作 git clone https://github.com/davatorium/rofi.git 根据网上的信息,rofi 中文输入主要是依赖 xcb-imdkit 这个库 当然我们是使用源 ...