延续:iOS开发基础-图片切换(1),对(1)里面的代码进行改善。

在 ViewController 类中添加新的数组属性:

   @property (nonatomic, strong) NSArray *infoArray; //存放图片信息

  通过 self.infoArray 的 getter 方法对其实现初始化(懒加载),其中代码中的 _infoArray 不能用 self.infoArray 替换:

 //infoArray的get方法
- (NSArray *)infoArray {
NSLog(@"需要获取图片信息数组");
//只实例化一次
if (_infoArray == nil) {
NSLog(@"实例化数组");
NSDictionary *dict0 = @{@"name":@"beauty0", @"desc":@"美女1号"};
NSDictionary *dict1 = @{@"name":@"beauty1", @"desc":@"美女2号"};
NSDictionary *dict2 = @{@"name":@"beauty2", @"desc":@"美女3号"};
NSDictionary *dict3 = @{@"name":@"beauty3", @"desc":@"美女4号"};
NSDictionary *dict4 = @{@"name":@"beauty4", @"desc":@"美女5号"};
_infoArray = @[dict0, dict1, dict2, dict3, dict4];
}
return _infoArray;
}

  修改 change 方法如下:

 //视图内容更新与显示
- (void)change {
//初始化两个标签和一个图片
self.imageIcon.image = [UIImage imageNamed:self.infoArray[self.i][@"name"]];
self.lastLabel.text = self.infoArray[self.i][@"desc"];
[self.firstLabel setText:[NSString stringWithFormat:@"%d/5", self.i+]]; self.leftButton.enabled = (self.i != ); //显示第1张图片时,左翻按钮失效
self.rightButton.enabled = (self.i != ); //显示第5张图片时,右翻按钮失效
}

参考博客:iOS开发UI篇—简单的浏览器查看程序

示例代码:http://pan.baidu.com/s/1XFj10

04-25 05:21