• 通过懒加载把最初的plist文件加载后,根据plist文件文件中的目标控制器进行跳转,根据加载的plist文件中的plist_name加载将要跳转进去的控制器界面的控件等等.

  • 以上根据target_vc创建对应的目标控制器

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSDictionary * group = self.groups[indexPath.section];
NSDictionary * item = [group[@"items"] objectAtIndex:indexPath.row];
NSString * targetClassName = item[@"target_vc"];
if (targetClassName) {
//字符串转类
Class TargetClass = NSClassFromString(targetClassName);
//所有控制器都继承自UIViewControler用他来接收,并创建对应目标控制器类型
UIViewController * destVc = [[TargetClass alloc] init];
if ([destVc isKindOfClass:[PBBSettingViewController class]]) {
PBBSettingViewController * settingVc = (PBBSettingViewController *)destVc; //如果点击的cell可以跳转新的"设置控制器"则,将group懒加载的self.plistname修改为要跳转的plist文件名,在这里则根据key--plist_name键取相应的plist文件名,进行跳转准备,准备内部控件
settingVc.plistName = item[@"plist_name"]; }
destVc.navigationItem.title = item[@"title"];
[self.navigationController pushViewController:destVc animated:YES];
} }
  • 如果目标控制器是类似设置的控制器,则强转为设置类型的控制器,由于继承其所有方法与属性,则可以加载对应的plist文件对齐内部控件进行赋值等操作.

  • 看图分析plist文件层级结构

  • 首先整个plist是个数组,数组内部对应的成员都是字典,字典内部对应的tableView的每组的成员 有 header 和 footer 以及内部的cell,所以字典内部存放的是以字典存储的header和footer以及以数组存放的cell们,每个cell又是一个字典用来取对应的图片资源 或者 创建对应的目标控制器以及目标plist文件.

  • 本文几乎全部是tableview,则通过懒加载来获取plist文件数据.通过数据源方法来对获取的数据进行解析--对控件进行创建以及赋值.

以下代码是控制代码,用来获取plist文件,把通过plist文件获取的资源赋给对应的tableView的控件



#import "PBBSettingViewController.h"
#import "PBBSettingCell.h" @interface PBBSettingViewController () @property(nonatomic,strong)NSArray * groups; @end @implementation PBBSettingViewController -(NSArray *)groups{ if (_groups == nil) {
NSString * path = [[NSBundle mainBundle] pathForResource:self.plistName ofType:nil];
_groups = [NSArray arrayWithContentsOfFile:path];
}
return _groups;
} #pragma mark - 数据源
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//多少个组
return self.groups.count;
} -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ //先获取组
NSDictionary * dict = self.groups[section];
//获取组中对应items的数量 -- 每组cell的数量
NSArray * arr = dict[@"items"];
return arr.count;
} -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSDictionary * group = self.groups[indexPath.section];
NSDictionary * item = [group[@"items"] objectAtIndex:indexPath.row]; //由于跳转的控制器的tableView的cell的sytle不同则在cell内部封装判断cell的style的方法.并且需要在编写plist文件中写上cell的style用来用key键取值去判断相应cell的style.item是获取一组cell中单个的cell以及属性. PBBSettingCell * cell = [PBBSettingCell settingCellWith:tableView item:item]; //调用了item的set方法,在PBBSettingCell.m文件中实现了item的set方法,在set方法中进行了对cell内部子控件(左边icon头像的判断,中间title,以及details,右边accessory的判断以及赋值)的赋值.
cell.item = item;
return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSDictionary * group = self.groups[indexPath.section];
NSDictionary * item = [group[@"items"] objectAtIndex:indexPath.row];
NSString * targetClassName = item[@"target_vc"];
if (targetClassName) {
//字符串转类
Class TargetClass = NSClassFromString(targetClassName);
//所有控制器都继承自UIViewControler用他来接收,并创建对应目标控制器类型
UIViewController * destVc = [[TargetClass alloc] init];
if ([destVc isKindOfClass:[PBBSettingViewController class]]) {
PBBSettingViewController * settingVc = (PBBSettingViewController *)destVc; //如果点击的cell可以跳转新的"设置控制器"则,将group懒加载的self.plistname修改为要跳转的plist文件名,在这里则根据key--plist_name键取相应的plist文件名,进行跳转准备,准备内部控件
settingVc.plistName = item[@"plist_name"]; }
destVc.navigationItem.title = item[@"title"];
[self.navigationController pushViewController:destVc animated:YES];
} } -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ NSDictionary * group = self.groups[section];
return group[@"footer"];
} -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ NSDictionary * group = self.groups[section];
return group[@"header"];
} //设置tableView为分组的样式,重写构造init方法
-(instancetype)init{ return [super initWithStyle:UITableViewStyleGrouped];
} -(instancetype)initWithStyle:(UITableViewStyle)style{ return [super initWithStyle:UITableViewStyleGrouped];
} //设置返回按钮 格式 不同状态下的显示图片
-(void)viewDidLoad{ [super viewDidLoad]; UIBarButtonItem * backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"NavBack"] style:UIBarButtonItemStylePlain target:self action:@selector(didClickBackButton)]; self.navigationItem.rightBarButtonItem.title = @"常见问题";
self.navigationItem.title = @"设置哦!";
self.navigationItem.leftBarButtonItem = backItem; }
//如果自定义了返回按钮,则返回按钮进入上一个控制器需要手动返回.
-(void)didClickBackButton{ [self.navigationController popViewControllerAnimated:YES]; } @end

以下是在cell.m文件中对传递进来的数据进行解析---创建控件---对控件进行赋值(控件头像,title,以及accessory类型等等的判断)


#import "PBBSettingCell.h" @implementation PBBSettingCell //通过plist文件中的cell_style来创建对应的cell的style
+(UITableViewCellStyle)cellStyleWithText:(NSString *)cellStyle{ if ([cellStyle isEqualToString:@"UITableViewCellStyleSubtitle"]) {
return UITableViewCellStyleSubtitle;
} else if ([cellStyle isEqualToString:@"UITableViewCellStyleValue2"]){ return UITableViewCellStyleValue2;
} else if([cellStyle isEqualToString:@"UITableViewCellStyleValue1"]){ return UITableViewCellStyleValue1;
} else {
return UITableViewCellStyleDefault;
}
} //这个有卵用??-------暂时没有用...
+(instancetype)settingCellWith:(UITableView *)tableView{ static NSString * ID = @"settigs_cell";
PBBSettingCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[PBBSettingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
} //颜色可以随便设置
cell.detailTextLabel.textColor = [UIColor colorWithRed:173.0/255.0 green:166.0/255.0 blue:144.0/255.0 alpha:1.0];
return cell;
}
+(instancetype)settingCellWith:(UITableView *)tableView item:(NSDictionary *)item{ static NSString * ID = @"settigs_cell";
PBBSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) { cell = [[PBBSettingCell alloc] initWithStyle:[self cellStyleWithText:item[@"cell_style"]] reuseIdentifier:ID];
}
cell.detailTextLabel.textColor = [UIColor colorWithRed:173.0/255.0 green:166.0/255.0 blue:144.0/255.0 alpha:1.0];
return cell;
} -(void)setItem:(NSDictionary *)item{ _item = item;
//判断是否有头像
if (item[@"icon"]) {
self.imageView.image = [UIImage imageNamed:item[@"icon"]];
} self.textLabel.text = item[@"title"];
self.detailTextLabel.text = item[@"details"];
//设置判断accessory
if (item[@"accessory"] && [item[@"accessory"] length]>0) {
//字符串转换为类
Class AccessoryClass = NSClassFromString(item[@"accessory"]);
id accessory_obj = [[AccessoryClass alloc] init];
//判断是否有图片框 没有图片框就是UISwitch类型或者是什么都没有...
if ([accessory_obj isKindOfClass:[UIImageView class]]) { UIImageView * imageView = (UIImageView *)accessory_obj;
imageView.image = [UIImage imageNamed:item[@"accessory_img"]];
[imageView sizeToFit];
}
self.accessoryView = accessory_obj;
} } - (void)awakeFromNib {
// Initialization code
} - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated]; // Configure the view for the selected state
} @end

总结:类似这种控制器的跳转,跳转后还需要进行跳转的"设置"类型的跳转,需要自己写plist文件的,而且跳转后的控制器的都基本是同一种控制器例如tableViewController,可以在plist文件中写入所有的tableView的属性,因为可能跳转进入不同的控制器界面对应的控件有所不同,有的就写上,创建什么类型的cell进行判断即可.

论--如何通过代码解析plist文件创建对应的控制器,以及控制器中的控件的更多相关文章

  1. C# WinForm中 让控件全屏显示的实现代码

    夏荣全 ( lyout(at)163.com )原文 C#中让控件全屏显示的实现代码(WinForm) 有时候需要让窗口中某一块的内容全屏显示,比如视频播放.地图等等.经过摸索,暂时发现两种可行方法, ...

  2. Android在代码中设置控件的drawableLeft,drawableRight,drawableTop,drawableBottom。

    根据业务的需要,要在代码中设置控件的drawableLeft,drawableRight,drawableTop,drawableBottom属性. 我们知道在xml中设置的方法为:android:d ...

  3. Webform中Repeater控件--绑定嵌入C#代码四种方式

    网页里面嵌入C#代码用的是<% %>,嵌入php代码<?php ?> 绑定数据的四种方式: 1.直接绑定 <%#Eval("Code") %> ...

  4. Qt中在UI文件中新建控件并命名,但在代码中无法识别UI中的控件?

    代码中添加FilePathLineEdit控件,显示标准文件选择对话框显示选择的文件路径,但在槽函数中ui->FilePathLineEdit->setText("FilePat ...

  5. C#代码中设置 控件的触发器

    Style style = new Style(); style.TargetType = typeof(TextBox); MultiDataTrigger trigger = new MultiD ...

  6. Android开发之常用框架WebView详解代码。超详细,送给初学者,完全掌握此控件

    这是我特意为新手小白写的一个代码,教大家完完全全掌握WebView, 我感觉,你看懂这个,基本上可以满足以后工作中的需要了,(只针对Webview的使用),但是其实它还有好多功能,比如真正的设计到和H ...

  7. iOS代码实现:创建按钮,绑定按钮事件,读取控件值

    // // main.m // Hello // // Created by lishujun on 14-8-28. // Copyright (c) 2014年 lishujun. All rig ...

  8. C#代码总结02---使用泛型来获取Asp前台页面全部控件,并进行属性修改

    该方法:主要用于对前台页面的不同类型(TextBox.DropDownList.等)或全部控件进行批量操作,用于批量修改其属性(如,Text.Enable). private void GetCont ...

  9. PHP代码中input控件使用id无法POST传值,使用name就可以

    <html> <head> <title>Example</title> </head> <body> <?php if ...

随机推荐

  1. POJ 2299 树状数组+离散化求逆序对

    给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...

  2. LR调用动态链接库DLL

    什么是动态库? 动态库一般又叫动态链接库(DLL),是Dynamic Link Library 的缩写形式,DLL是一个包含可由多个程序同时使用的代码和数据的库. 动态链接提供了一种方法 ,使进程可以 ...

  3. git rm –cached filename

    为了能重新忽略那些已经被track的文件,例如停止tracking一个文件但是又不从仓库中删除它.可以使用以下命令: 代码如下 git rm –cached filename 上面这个命令用于删除单个 ...

  4. JAVA基础讲义

    一.安装JDK 第一步:双击JDK的exe文件. JDK(Java开发包),JRE(Java运行环境) 第二步:配置 path:jdk的根目录,jdk下的bin目录(两个目录之间记得用分号隔开) cl ...

  5. 蓝牙—服务发现协议(SDP)

    服务搜索协议(SDP)提供了应用发现可用服务以及确定可用服务特点的方法.SDP发现协议提供下面的能力 <1>为客户提供搜索所需要服务的能力. <2>允许基于服务类型搜索服务 & ...

  6. 小易邀请你玩一个数字游戏,小易给你一系列的整数。你们俩使用这些整数玩游戏。每次小易会任意说一个数字出来,然后你需要从这一系列数字中选取一部分出来让它们的和等于小易所说的数字。 例如: 如果{2,1,2,7}是你有的一系列数,小易说的数字是11.你可以得到方案2+2+7 = 11.如果顽皮的小易想坑你,他说的数字是6,那么你没有办法拼凑出和为6 现在小易给你n个数,让你找出无法从n个数中选取部分求和

    小易邀请你玩一个数字游戏,小易给你一系列的整数.你们俩使用这些整数玩游戏.每次小易会任意说一个数字出来,然后你需要从这一系列数字中选取一部分出来让它们的和等于小易所说的数字. 例如: 如果{2,1,2 ...

  7. sphinx续4-coreseek的工作原理

    原文地址:http://blog.itpub.net/29806344/viewspace-1399621/ 在分析sphix原理之前,我先澄清一下为什么经常出现coreseek这个词? 因为sphi ...

  8. To do

    小事{ android values public.xml 树.图的所有遍历方式和优劣 } 大事{ 通读android所有官网文档. android多dex多res开发框架. java AOT(and ...

  9. ArcMap打开越来越慢

    原文:ArcMap打开越来越慢 今天终于找到原因了,原来是 C:\Users\Administrator\AppData\Roaming\ESRI\Desktop10.1\ArcToolbox下 Ar ...

  10. GitHub 客户端

    GitHub客户端下载官网:https://desktop.github.com/ GitHubFlow:https://guides.github.com/introduction/flow/ 客户 ...