码迷,mamicode.com
首页 > 移动开发 > 详细

iOS -- 图片浏览器2(用NSDictionary来存储数据)

时间:2015-09-17 16:56:28      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

// strong : 一般对象  weak : UI控件
#define IconKey @"icon"
#define DescKey @"desc"
#import "ViewController.h"
@interface ViewController ()
- (IBAction)previous;
- (IBAction)next;
@property (weak, nonatomic) IBOutlet UIButton *previousBtn;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
@property (weak, nonatomic) IBOutlet UILabel *noLabel;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *descLabel;
// 记录当前显示的是第几张图片
@property (nonatomic, assign) int index;
// 图片数据集合
@property (nonatomic, strong) NSArray *imageData;
@end
@implementation MJViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self changeData];
}
- (NSArray *)imageData
{
    if (_imageData == nil) { // 从未初始化
        // 初始化数据
        NSMutableDictionary *image1 = [NSMutableDictionary dictionary];
        image1[IconKey] = @"biaoqingdi";
        image1[DescKey] = @"在他面前,其他神马表情都弱爆了!";
        NSMutableDictionary *image2 = [NSMutableDictionary dictionary];
        image2[IconKey] = @"wangba";
        image2[DescKey] = @"哥们为什么选八号呢";
        NSMutableDictionary *image3 = [NSMutableDictionary dictionary];
        image3[IconKey] = @"bingli";
        image3[DescKey] = @"这也忒狠了";
        NSMutableDictionary *image4 = [NSMutableDictionary dictionary];
        image4[IconKey] = @"chiniupa";
        image4[DescKey] = @"chiniupa";
        NSMutableDictionary *image5 = [NSMutableDictionary dictionary];
        image5[IconKey] = @"danteng";
        image5[DescKey] = @"亲,你能改下你的网名么?哈哈";
//        NSMutableDictionary *image6 = [NSMutableDictionary dictionary];
//        image6[IconKey] = @"chiniupa";
//        image6[DescKey] = @"新增的数据";
        _imageData = @[image1, image2, image3, image4, image5];
    }
    return _imageData;
}
#pragma mark 改变数据
- (void)changeData
{
    // 1.改变数据
    self.noLabel.text = [NSString stringWithFormat:@"%d/%d", self.index + 1, self.imageData.count];
    // 2.取出index对应的字典数据
    NSDictionary *imageDict = self.imageData[self.index];
    // 3.设置图片
    self.iconView.image = [UIImage imageNamed:imageDict[IconKey]];
    // 4.设置描述
    self.descLabel.text = imageDict[DescKey];
    // 2.改变按钮状态
    self.previousBtn.enabled = (self.index != 0);
    self.nextBtn.enabled = (self.index != self.imageData.count - 1);
}
#pragma mark 上一张
- (IBAction)previous {
    // 1.减小索引
    self.index--;
    // 2.改变数据
    [self changeData];
}
#pragma mark 下一张
- (IBAction)next {
    // 1.增加索引
    self.index++; //0
    // 2.根据索引显示对应的内容
    [self changeData];
}
@end

iOS -- 图片浏览器2(用NSDictionary来存储数据)

标签:

原文地址:http://www.cnblogs.com/lianfu/p/4816433.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!