標籤:
// 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來儲存資料)