码迷,mamicode.com
首页 > 移动开发 > 详细

iOS-UI篇—简单的浏览器查看程序

时间:2015-12-27 23:13:12      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

 1 #import "ViewController.h"
 2 
 3 @interface ViewController ()
 4 
 5 @property (retain, nonatomic) NSArray *pic;
 6 @property (assign, nonatomic) NSInteger index;
 7 @property (weak, nonatomic) IBOutlet UILabel *lblIndex;
 8 @property (weak, nonatomic) IBOutlet UILabel *lblTitle;
 9 @property (weak, nonatomic) IBOutlet UIImageView *picture;
10 - (IBAction)nextPicture:(id)sender;
11 @property (weak, nonatomic) IBOutlet UIButton *btnnext;
12 
13 - (IBAction)lastPicture:(id)sender;
14 @property (weak, nonatomic) IBOutlet UIButton *btnLast;
15 
16 @end
17 @implementation ViewController
18 
19 
20 - (void)viewDidLoad {
21     [super viewDidLoad];
22     //让第一个图片显示出来,所以先调用一次
23     [self nextPicture:nil];
24 }
25 
26 - (void)didReceiveMemoryWarning {
27     [super didReceiveMemoryWarning];
28 }
29 
30 // 重写pic属性get方法---懒加载数据
31 - (NSArray *)pic
32 {   /*
33      写代码加载picture.plist文件中的数据到_pic;
34      1.获取picture.plist文件的路径
35      ps:[NSBundle mainBundle]获取这个app安装到手机上时的根目录
36      然后在根目录下搜索picture.plist文件路径
37      */
38     if (_pic == nil) {
39         NSString *string = [[NSBundle mainBundle] pathForResource:@"picture" ofType:@".plist"];
40         NSArray *array = [NSArray arrayWithContentsOfFile:string];
41         //NSLog(@"%@",array);
42         
43         _pic = array;
44         
45     }
46     return _pic;
47 }
48 
49 - (IBAction)nextPicture:(id)sender {
50     //1.让索引自加
51     _index++;
52     //2.从数组中获取当前这张图片的数据
53     NSDictionary *dict = self.pic[self.index-1];
54     //3.把获取到得数据设置给界面上的控件
55     self.lblIndex.text = [NSString stringWithFormat:@"%ld/%ld",self.index,self.pic.count];
56     //4.通过image属性来设置图片框里面的图片
57     self.picture.image =[UIImage imageNamed: dict[@"picture"]];
58     
59     self.lblTitle.text = dict[@"title"];
60     //设置“下一张”按钮是否可以点击
61     self.btnnext.enabled =( _index != self.pic.count);
62     //设置“上一张”按钮是否可以点击
63     self.btnLast.enabled =( _index-1 != 0);
64 
65 }
66 
67 - (IBAction)lastPicture:(id)sender {
68     _index--;
69     NSDictionary *dict = self.pic[self.index-1];
70     self.lblIndex.text = [NSString stringWithFormat:@"%ld/%ld",self.index,self.pic.count];
71     self.picture.image =[UIImage imageNamed: dict[@"picture"]];
72     self.lblTitle.text = dict[@"title"];
73     
74     self.btnLast.enabled =( _index-1 != 0);
75     self.btnnext.enabled =( _index != self.pic.count);
76 }
77 @end

 

开发思路:

1.完成基本功能

2.考虑性能

(1)(初始化操作,可以直接调用change进行)

(2)因为要控制序号和图片两个变量,所以考虑使用字典代替掉switch

(3)每次点击,字典都需要创建一次,效率地下,可以考虑创建的这部分拿到初始化方法中去,这样就只需要创建一次就ok了。

(4)考虑缺点(对代码的顺序要求极其严格)

(5)懒加载(需要的时候才加载,那么什么时候是需要的时候,及调用get方法的时候)

(6)每次都来一下?效率低下—》只有第一次调用get方法时为空,此时实例化并建立数组,其他时候直接返回成员变量(仅仅执行一次)

注意点:

1.方法的调用堆栈(顺序)。

2.使用plist:让数据的操作更加灵活,把数据弄到外面去,解除耦合性,让耦合性不要太强。实际上是一个xml,是苹果定义的一种特殊格式的xml。

3.bundle-包(只读)

iOS-UI篇—简单的浏览器查看程序

标签:

原文地址:http://www.cnblogs.com/oc-bowen/p/5080978.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!