图片浏览器 - OC

import "ViewController.h"

@interface ViewController ()

//设置一个数组来储存信息
@property (nonatomic,strong) NSArray *pic;
//设置一个索引器
@property (nonatomic,assign) int index;

@property (weak, nonatomic) IBOutlet UILabel *CONumber;
@property (weak, nonatomic) IBOutlet UIImageView  *COImage;
@property (weak, nonatomic) IBOutlet UILabel *COTitle;
@property (weak, nonatomic) IBOutlet UIButton *next;
@property (weak, nonatomic) IBOutlet UIButton *perv;

- (IBAction)nextAction;
- (IBAction)prevAction;

@end

@implementation ViewController

//重写pic的get方法
- (NSArray *)pic{

if (_pic == nil) {
    //获取文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"pic.plist" ofType:nil];
    //取得路径文件
    NSArray *array = [NSArray arrayWithContentsOfFile:path];

    _pic = array;
}

return _pic;
}

- (void)viewDidLoad {
[super viewDidLoad];

self.index = - 1;

[self nextAction];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)nextAction{

//1.索引++
self.index++;

//2.从数组中取出当前图片的信息
NSDictionary *dic = self.pic[self.index];

//3.给控件赋值
self.CONumber.text = [NSString stringWithFormat:@"%d/%ld",self.index + 1,self.pic.count];
self.COImage.image = [UIImage imageNamed:dic[@"icon"]];
self.COTitle.text = dic[@"title"];

self.next.enabled = (self.index != (self.pic.count - 1));
self.perv.enabled = (self.index != 0);

}
- (IBAction)prevAction{

//1.索引++
self.index--;

//2.从数组中取出当前图片的信息
NSDictionary *dic = self.pic[self.index];

//3.给控件赋值
self.CONumber.text = [NSString stringWithFormat:@"%d/%ld",self.index + 1,self.pic.count];
self.COImage.image = [UIImage imageNamed:dic[@"icon"]];
self.COTitle.text = dic[@"title"];

self.next.enabled = (self.index != (self.pic.count + 1));
self.perv.enabled = (self.index != 0);
}

@end

你可能感兴趣的:(图片浏览器 - OC)