首页 > 代码库 > iOS-图片浏览器(字典和plist文件的使用)

iOS-图片浏览器(字典和plist文件的使用)

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #822d0f } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c81b13 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000; min-height: 21.0px } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3c828b } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #21961c } p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #21961c } p.p8 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c42275 } p.p9 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } p.p10 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3e1e81 } p.p11 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #539aa4 } p.p12 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #294c50 } p.p13 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #1e9421 } span.s1 { } span.s2 { font: 18.0px "PingFang SC" } span.s3 { color: #c81b13 } span.s4 { color: #822d0f } span.s5 { color: #c42275 } span.s6 { color: #000000 } span.s7 { font: 18.0px Menlo } span.s8 { color: #6122ae } span.s9 { color: #539aa4 } span.s10 { color: #0435ff } span.s11 { font: 18.0px Menlo; color: #000000 }</style>

//

//  ViewController.m

//  19-图片浏览器

//

//  Created by hongqiangli on 2017/7/31.

//  Copyright ? 2017 李洪强. All rights reserved.

//

#define icon @"icon"

#define desc @"desc"

#import "ViewController.h"

 

@interface ViewController ()

 

/**

 记录当前的索引号

 */

@property(nonatomic,assign)int index;

 

/**

 上一张

 */

- (IBAction)previous;

 

/**

 下一张

 */

- (IBAction)next;

 

/**

 描述

 */

@property (weak, nonatomic) IBOutlet UILabel *descLabel;

 

/**

 上一个按钮

 */

@property (weak, nonatomic) IBOutlet UIButton *previousBtn;

 

/**

 下一个按钮

 */

@property (weak, nonatomic) IBOutlet UIButton *nextBtn;

 

/**

 索引label

 */

@property (weak, nonatomic) IBOutlet UILabel *noLabel;

 

@property (weak, nonatomic) IBOutlet UIImageView *mainImageView;

 

/**

 一般的OC对象用strong修饰

 */

@property(nonatomic,strong)NSArray *images;

 

@end

 

@implementation ViewController

- (NSArray *)images{

    if(_images == nil){

        //字典的方式初始化

//        NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];

//        dic1[icon] = @"biaoqingdi";

//        dic1[desc] = @"在他面前,其他神马表情都弱爆了!";

//        NSMutableDictionary *dict2 = [NSMutableDictionary dictionary];

//        dict2[icon]=  @"wangba" ;

//        dict2[desc] = @"哥们为什么选八号呢";

//        NSMutableDictionary *dict3 = [NSMutableDictionary dictionary];

//        dict3[icon]=  @"bingli" ;

//        dict3[desc] = @"这也忒狠了!";

//        NSMutableDictionary *dict4 = [NSMutableDictionary dictionary];

//        dict4[icon]=  @"chiniupa" ;

//        dict4[desc] = @"这小姑娘吃个牛排比杀牛还费劲啊";

//        NSMutableDictionary *dict5 = [NSMutableDictionary dictionary];

//        dict5[icon]=  @"danteng" ;

//        dict5[desc] = @"亲,你能改下你的网名么?哈哈";

//        _images = @[dic1,dict2,dict3,dict4,dict5];

        

        //plist的方式加载

        //拿到plist文件的全路径

        NSString *path = [[NSBundle mainBundle]pathForResource:@"images" ofType:@"plist"];

        _images = [NSArray arrayWithContentsOfFile:path];

        

    }

    

    

    return _images;

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

    [self changData];

    

}

 

/**

 点击上一张

 */

- (IBAction)previous {

    _index--;

    [self changData];

}

 

/**

 下一张

 */

- (IBAction)next {

    _index++;

    [self changData];

    

}

 

- (void)changData{

    _noLabel.text = [NSString stringWithFormat:@"%d/%ld",_index + 1,_images.count];

    NSDictionary *dict = _images[_index];

    NSString *iconStr = dict[icon];

    NSString *descStr = dict[desc];

    _mainImageView.image = [UIImage imageNamed:iconStr];

    _descLabel.text = descStr;

    //判断按钮能不能点击

    _previousBtn.enabled = (_index != 0);

    _nextBtn.enabled = (_index != 4);

 

}

@end

iOS-图片浏览器(字典和plist文件的使用)