iOS -- 圖片瀏覽器2(用NSDictionary來儲存資料)

來源:互聯網
上載者:User

標籤:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.