ios开发进阶-笔记iOS 开发 iOS开发

Plist文件加载控制器一(设置界面怎么写)

2016-05-30  本文已影响200人  记住你姓李

相信很多朋友都写过这样的设置界面,点击每个cell 都会调到相应的新的控制器,如果说每个控制器我都要在里面进行重写布局,那么代码量肯定会非常的大而且阅读性也不是很好.

就像这样(下图),这里push的没一个控制器都是一个单独的控制器 , 这个设置界面包含五个控制器 , 每个里面又要单独的进行设置 , 这个还算是少的,如果说这个设置界面进去后 , 还可以跳转要是还这样写那真的是 ........ 醉了 , 可能你整个项目的界面数量基本2/3 都在设置界面上了.大大的提高了工作效率,而且程序的可靠性可能也会受到相应的影响

这个问题困扰我很久(上面这个就是我写的 醉了当时着急  没时间细想  就先把功能实现再说)想找相关的技术博客发现这方面的博客实在是太少了终于让我找到了解决办法(sorry,又不要脸了,看到了个视频里面的方法) plist文件加载控制器 而且可以为控制器进行设置,本来可以写跟设置界面一样的,但是周末犯懒了,等我想写的时候已经12点多了,没办法周一还要上班就写了个简易版的,先对付看吧下周补全(下面就是plist文件,自己写)

```

排版有点问题 , 下次肯定改进

#import "ViewController.h"#import "SetingViewController.h"@interface ViewController ()@property (nonatomic,strong)NSArray *arr;

@end

@implementation ViewController

-(NSArray *)arr{

if (!_arr) {

NSString *path = [[NSBundle mainBundle] pathForResource:@"SetingV_C.plist" ofType:nil];

_arr = [NSArray arrayWithContentsOfFile:path];

}

return _arr;

}

- (void)viewDidLoad {

[super viewDidLoad];

NSLog(@"%lu",(unsigned long)self.arr.count);

UITableView *tab = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];

[self.view addSubview:tab];

tab.delegate = self;

tab.dataSource = self;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return self.arr.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *ID = @"cell";

NSDictionary *dict = self.arr[indexPath.row];

NSLog(@"%@",dict);

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

if (cell == nil) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];

}

cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:dict[@"accview_img"]]];

cell.accessoryView.backgroundColor = [UIColor blackColor];

cell.textLabel.text = @"cell";

return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

return 80;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

NSDictionary *dict = self.arr[indexPath.row];

NSString *TargetClassName = dict[@"setingV_c"];

if (TargetClassName) {

Class TargetClass = NSClassFromString(TargetClassName);

UIViewController *vc = [[TargetClass alloc]init];

if ([vc isKindOfClass:[SetingViewController class]]) {

SetingViewController *setVc = (SetingViewController *)vc;

[self.navigationController pushViewController:setVc animated:YES];

NSLog(@"%@",[setVc class]);

}

}

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

```

上面看不清楚的 看这里吧  首先懒加载吧plist文件加载到数组中(数组中是字典  tabView的数据源和代理方法就不粘贴了 基本就那样)

点击cell的时候  通过数组找到相应的键值,再把他们通过映射转正类然后就可以直接跳转,页面布局相近的控制器可以重复使用,只需要修改(换一个plist)为他重新设置页面就可以使用

(下图就是这个代码的运行状态,accessoryView 添加的图片没时间找我就自己截了个图演示用用就OK,这些都是通过plist文件加载的,除了文字cell, 那个是着急了,没时间弄) 如果需要我们调到下个控制器还可以用这样的布局,对应的accessoryView 或者其他的cell中的显示的内容我们可以通过修改plist文件就可以进行修改

下周六更新,这里只是一个简单的跳转到下一个控制器,明天还要上班就先到这里~
上一篇 下一篇

猜你喜欢

热点阅读