美文网首页
图片浏览器 - OC

图片浏览器 - OC

作者: 以太死多 | 来源:发表于2015-09-07 23:03 被阅读463次

    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

          本文链接:https://www.haomeiwen.com/subject/rdaucttx.html