图片浏览器

xiaoxiao2021-02-27  211

#import "ViewController.h" @interface ViewController () @property (nonatomic, strong) NSArray *pic; //自己写一个索引,来控制当前显示的是第几张图片 //这个属性一开始没有赋值就是0 @property (nonatomic, assign) int index; - (IBAction)next; @property (weak, nonatomic) IBOutlet UILabel *lblIndex; @property (weak, nonatomic) IBOutlet UIImageView *imgViewIcon; @property (weak, nonatomic) IBOutlet UILabel *lblTitle; @property (weak, nonatomic) IBOutlet UIButton *btnNaxt; - (IBAction)pre; @property (weak, nonatomic) IBOutlet UIButton *btnpre; @end @implementation ViewController //重写pic属性的get方法 //------------懒加载数据------------ -(NSArray *)pic { if (_pic == nil) { //写代码加载pic.plist文件中的数据到_pic //1.获取pic.plist文件的路径 //获取pic.plist文件的路径赋值给path变量 //[NSBundle mainBundle]表示获取这个app安装到手机时的根目录 //然后在app的安装的根目录下搜索pic.plist文件的路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"pic.plist" ofType:nil]; //读取文件 NSArray *array = [NSArray arrayWithContentsOfFile:path]; _pic = array; //NSLog(@"count: %ld", array.count); //NSLog(@"%@", array); } return _pic; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //下一张图片 - (IBAction)next { //1.让索引++ self.index++; //2.从数组中获取当前这张图片的数据 NSDictionary *dict = self.pic[self.index]; //3.把获取到的数据设置给界面上的控件 self.lblIndex.text = [NSString stringWithFormat:@"%d/%ld", self.index+1, self.pic.count]; //通过image属性来设置图片框里面的图片 self.imgViewIcon.image = [UIImage imageNamed:dict[@"icon"]]; //设置张图片的标题 self.lblTitle.text = dict[@"title"]; //设置“下一张”按钮是否可点击 //方法一: self.btnNaxt.enabled = (self.index != (self.pic.count - 1)); self.btnpre.enabled = (self.index != 0); /* 方法二: if (self.index == (self.pic.count -1)) { self.btnNaxt.enabled = NO; }else{ self.btnNaxt.enabled = YES; } */ } //显示上一张 - (IBAction)pre { //1.让索引++ self.index--; //2.从数组中获取当前这张图片的数据 NSDictionary *dict = self.pic[self.index]; //3.把获取到的数据设置给界面上的控件 self.lblIndex.text = [NSString stringWithFormat:@"%d/%ld", self.index+1, self.pic.count]; //通过image属性来设置图片框里面的图片 self.imgViewIcon.image = [UIImage imageNamed:dict[@"icon"]]; //设置张图片的标题 self.lblTitle.text = dict[@"title"]; //设置“显示上一张”按钮是否可点击 //方法一: self.btnpre.enabled = (self.index != 0); self.btnNaxt.enabled = (self.index != (self.pic.count - 1)); /* 方法二: if (self.index == 0) { self.btnpre.enabled = NO; }else{ self.btnpre.enabled = YES; } */ } @end
转载请注明原文地址: https://www.6miu.com/read-9651.html

最新回复(0)