【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/ ...
随机推荐
- 习题8 #第8章 Verilog有限状态机设计-1 #Verilog #Quartus #modelsim
1. 设计一个"111"串行数据检测器.要求是:当检测到连续3个或3个以上的"1"时输出为1,其他输入情况下输出为0. (1)思路分析:参照本章前文的范例,如第 ...
- 记录一个vue路由拦截效果的小技巧
使用一句三元表达式, <router-link class="flex-left left" tag="div" :to="loginState ...
- c#胖东来小程序自动购物程序(接单,windows桌面程序、linux程序、网络应用等等)
一.程序效果 自动打开胖东来小程序,自动购物 二.实现 先截屏,然后利用opencv库识别下一步按键所在位置,然后使用mouse_event控制鼠标,模拟人的动作 第一步,截取屏幕 static Bi ...
- ruby on rails中sidekiq的使用
参考文章: https://www.jianshu.com/p/7ea473097023 安装redis 配置redis 配置sidekiq所依赖的redis位置,必须同时定义sidekiq的serv ...
- mac本地搭建ollama
mac本地搭建ollama webUI *简介:ollama-webUI是一个开源项目,简化了安装部署过程,并能直接管理各种大型语言模型(LLM).本文将介绍如何在你的macOS上安装Ollama服务 ...
- 【HarmonyOS NEXT】获取卸载APP后不变的设备ID
1. 背景 在HarmonyOS NEXT中,想要获取设备ID,有3种方式 UDID:deviceinfo.udid,仅限系统应用使用 AAID: aaid.getAAID(),然而卸载APP/恢复设 ...
- Linux搭建ESP-IDF开发环境
下载esp-gitee-tools git clone git@gitee.com:EspressifSystems/esp-gitee-tools.git 替换github网址 cd esp-git ...
- 不关闭SELinux情况下使用ftp传输
在做搭建ftp服务器的作业时,整了一个活,在不关闭SELinux的情况下测试ftp服务器 使用的环境,虚拟机*2 (CentOS 7),Hyper-v,网卡已设为静态 需要安装的软件包: 服务器(下称 ...
- 23ai中的True Cache到底能做啥?
最近,Oracle的产品管理总监在Oracle数据库内幕中介绍了True Cache. 原文链接如下: https://blogs.oracle.com/database/post/introduci ...
- Swift 与 Objc Exception
一.背景 Swift具备完善的Error handle机制,对于纯Swift下面的Error,在编码的时候能够正确处理. 在使用try? 处理抛出Error的方法的时候,会忽略Error,直接返回ni ...