效果图:
//
// LiuJieViewController.m
// 04-图片浏览器
//
// Created by XinYou on 15-1-30.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "LiuJieViewController.h"
// 使用宏来定义常量
#define LiuJieIconKey @"icon"
#define LiuJieDescKey @"desc"
@interface LiuJieViewController ()
- (IBAction)previous;
- (IBAction)next;
@property (weak, nonatomic) IBOutlet UILabel *numLabel;
@property (weak, nonatomic) IBOutlet UILabel *descLabel;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UIButton *previousBtn;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
// 记录当前显示的是第几张图片
@property (nonatomic, assign) int index;
// 图片数据集合。为什么这里使用strong策略?
// 暂时只需记住一点:strong策略用于一般对象,weak策略用于UI控件
@property (nonatomic, strong) NSArray *imageData;
@end
@implementation LiuJieViewController
// 使用延迟加载,没有必要在程序一运行就加载所有数据,而是需要用到某些数据时再加载这些数据。
- (NSArray *)imageData{
//如果这里写成 "self.imageData != nil" 就会陷入死循环
if(_imageData == nil){
NSMutableDictionary *image1 = [NSMutableDictionary dictionary];
image1[LiuJieIconKey] = @"biaoqingdi";
image1[LiuJieDescKey] = @"在他面前,其他神马表情都弱爆了!";
NSMutableDictionary *image2 = [NSMutableDictionary dictionary];
image2[LiuJieIconKey] = @"wangba";
image2[LiuJieDescKey] = @"哥们为什么选八号呢";
NSMutableDictionary *image3 = [NSMutableDictionary dictionary];
image3[LiuJieIconKey] = @"bingli";
image3[LiuJieDescKey] = @"这也忒狠了";
NSMutableDictionary *image4 = [NSMutableDictionary dictionary];
image4[LiuJieIconKey] = @"chiniupa";
image4[LiuJieDescKey] = @"chiniupa";
NSMutableDictionary *image5 = [NSMutableDictionary dictionary];
image5[LiuJieIconKey] = @"danteng";
image5[LiuJieDescKey] = @"亲,你能改下你的网名么?哈哈";
// 初始化数组
_imageData = @[image1, image2, image3, image4, image5];
}
return _imageData;
}
- (void)changeData{
// 1,改变页码
self.numLabel.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[LiuJieIconKey]];
// 4,设置图片的描述
self.descLabel.text = imageDict[LiuJieDescKey];
// 5,改变按钮状态
self.previousBtn.enabled = (self.index != 0);
self.nextBtn.enabled = (self.index != self.imageData.count-1);
}
- (void)viewDidLoad
{
[super viewDidLoad];
//
[self changeData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)previous {
self.index--;// self.index = self.index - 1; 这里既用到了set方法又用到了get方法
[self changeData];
}
- (IBAction)next {
self.index++;
[self changeData];
}
@end