【iOS发展-44】通过案例谈iOS重构:合并、格式化输出、宏观变量、使用数组来存储数据字典,而且使用plist最终的知识

我们今天的情况下是第一个例子,下面的5一来通过切换页上一页下一页:



(1)第一步,基本是以非常傻非常直接的方式来创建。这里用到的主要点有:

——把对象变量设置为全局变量使得能够在其它方法中调用来设置它们的属性

——设置了一个全局变量index,默认是0。然后通过添加降低这个index值并结合switch来调用不同的数据。

——利用先调用一次change方法初始化页面。使得页面定格在第一帧。

——利用button的enabled属性来设置button能否够被点击,然后结合index的值分别在第1张和第5张时分别把上一张和下一张button设置为灰色不可点击。当然初始化页面的时候也要推断一下把上一张button设置为灰色。

——这里的change方法和btnCheck方法都是代码重构的产物,由于这两个方法都须要在preOne和nextOne方法中被调用,所以为了避免反复代码,所以把这些反复的部分都封装成了方法。

#import "ViewController.h"
//把一些对象定义成全局变量。这样能够多个方法中调用
@interface ViewController (){
    UIButton *btnPre;
    UIButton *btnNext;
    UILabel *numLabel;
    UILabel *descLabel;
    UIImageView *imgView1;
    int index1;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    
    btnPre=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnPre.frame=CGRectMake(50, 150, 60, 30);
    [btnPre setTitle:@"上一张" forState:UIControlStateNormal];
    [btnPre addTarget:self action:@selector(preOne) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnPre];
    btnNext=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnNext.frame=CGRectMake(250, 150, 60, 30);
    [btnNext setTitle:@"下一张" forState:UIControlStateNormal];
    [btnNext addTarget:self action:@selector(nextOne) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnNext];
    
    numLabel=[[UILabel alloc]init];
    numLabel.frame=CGRectMake(170, 50, 100, 30);
    [self.view addSubview:numLabel];
    
    descLabel=[[UILabel alloc]init];
    descLabel.frame=CGRectMake(110, 300, 200, 30);
    [self.view addSubview:descLabel];
    
    imgView1=[[UIImageView alloc]init];
    imgView1.frame=CGRectMake(130, 100, 100, 100);
    [self.view addSubview:imgView1];
    
    //由于一開始的index为0。所以我们直接调用change方法,相当于把第一帧的页面调出来初始化页面
    [self change];
    
    btnPre.enabled=(index1!=0);
    
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)preOne{
    index1--;
    [self btnCheck];
    [self change];
}

-(void)nextOne{
    index1++;
    [self btnCheck];
    [self change];
}

//按钮的enabled属性,假设是NO,则变成灰色。

事实上下面语句是三目运算推到而来 -(void)btnCheck{ btnPre.enabled=(index1!=0); btnNext.enabled=(index1!=4); } -(void)change{ switch (index1) { case 0: numLabel.text=@"1/5"; descLabel.text=@"This is the letter A"; imgView1.image=[UIImage imageNamed:@"a.png"]; break; case 1: numLabel.text=@"2/5"; descLabel.text=@"This is the letter B"; imgView1.image=[UIImage imageNamed:@"b.png"]; break; case 2: numLabel.text=@"3/5"; descLabel.text=@"This is the letter C"; imgView1.image=[UIImage imageNamed:@"c.png"]; break; case 3: numLabel.text=@"4/5"; descLabel.text=@"This is the letter D"; imgView1.image=[UIImage imageNamed:@"d.png"]; break; case 4: numLabel.text=@"5/5"; descLabel.text=@"This is the letter E"; imgView1.image=[UIImage imageNamed:@"e.png"]; break; default: break; } } @end


(2)对switch部分进行改造:利用格式化输出重构代码:用第二行代码取代凝视掉的那5行代码。

-(void)change{
    numLabel.text=[NSString stringWithFormat:@"%d/%d",index1+1,5];
    switch (index1) {
        case 0:
            //numLabel.text=@"1/5";
            descLabel.text=@"This is the letter A";
            imgView1.image=[UIImage imageNamed:@"a.png"];
            break;
        case 1:
            //numLabel.text=@"2/5";
            descLabel.text=@"This is the letter B";
            imgView1.image=[UIImage imageNamed:@"b.png"];
            break;
        case 2:
            //numLabel.text=@"3/5";
            descLabel.text=@"This is the letter C";
            imgView1.image=[UIImage imageNamed:@"c.png"];
            break;
        case 3:
            //numLabel.text=@"4/5";
            descLabel.text=@"This is the letter D";
            imgView1.image=[UIImage imageNamed:@"d.png"];
            break;
        case 4:
            //numLabel.text=@"5/5";
            descLabel.text=@"This is the letter E";
            imgView1.image=[UIImage imageNamed:@"e.png"];
            break;
        default:
            break;
    }
}

(3)利用字典和数组把数据单独出来,并实现数据的删减和代码之间的独立,即增减数据后,不须要改动我们显示“总页数”等这些代码,但增减数据仍须要通过增减代码来实现。(在开头定义一个全局变量NSArray *arr1)

- (void)viewDidLoad {
    ……
    NSMutableDictionary *dic1=[NSMutableDictionary dictionary];
    dic1[@"icon"]=@"a.png";
    dic1[@"desc"]=@"This is the letter A";
    
    NSMutableDictionary *dic2=[NSMutableDictionary dictionary];
    dic2[@"icon"]=@"b.png";
    dic2[@"desc"]=@"This is the letter B";
    
    NSMutableDictionary *dic3=[NSMutableDictionary dictionary];
    dic3[@"icon"]=@"c.png";
    dic3[@"desc"]=@"This is the letter C";
    
    NSMutableDictionary *dic4=[NSMutableDictionary dictionary];
    dic4[@"icon"]=@"d.png";
    dic4[@"desc"]=@"This is the letter D";
    
    NSMutableDictionary *dic5=[NSMutableDictionary dictionary];
    dic5[@"icon"]=@"e.png";
    dic5[@"desc"]=@"This is the letter E";
    
    arr1=[NSArray arrayWithObjects:dic1,dic2,dic3,dic4,dic5, nil];
    //以上代码须要加入在self change上。否则这个初始化是没有数据能够初始化的
    [self change];
    
    btnPre.enabled=(index1!=0);
    
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

初始化了数据之后。其它地方都能够进行简化了。比方计算页数,比方取数据,就能够利用字典和数组来取数据:

-(void)btnCheck{
    btnPre.enabled=(index1!=0);
    btnNext.enabled=(index1!=arr1.count-1);//计算页数的相关代码
}

-(void)change{
    numLabel.text=[NSString stringWithFormat:@"%d/%d",index1+1,5];
    //取出相应的数据
    NSDictionary *dic=arr1[index1];
    //设置icon图片
    imgView1.image=[UIImage imageNamed:dic[@"icon"]];
    //设置描写叙述文字
    descLabel.text=dic[@"desc"];
}

以上一步的优点在于我们增减数据的时候仅仅须要在增减新的dic6等等。然后把dic6之类的加入到数组arr1中就可以。

其余地方不须要手动改动数字,由于我们引用数字的地方是用了数组的count属性。引用数据的地方使用了数组和字典的相关属性,不是写死的,而是活的。


(4)利用宏变量避免代码出现错误的可能性,尤其是在多人协作开发时。利用宏变量的提示可降低误输入的可能。

#define kICONKEY @"icon"
#define kDESCRIP @"desc"

所以其它响应的地方都应该替换成宏变量。


(5)利用property创建变量,注意。尽管老师说建议控件对象用weak。而一般对象用strong,可是发现。使用weak根本无法实例化对象,所以此处临时还是用strong。等查明原因再说。

响应的以下的变量都能够用_****取代或者用self.***取代。

建议此处用后者。

@interface ViewController (){
//    UIButton *btnPre;
//    UIButton *btnNext;
//    UILabel *numLabel;
//    UILabel *descLabel;
//    UIImageView *imgView1;
//    int index1;
}
@property(nonatomic,retain) UIButton *btnPre;
@property(nonatomic,retain) UIButton *btnNext;
@property(nonatomic,retain) UILabel *numLabel;
@property(nonatomic,retain) UILabel *descLabel;
@property(nonatomic,strong) UIImageView *imgView1;
@property(nonatomic,strong) NSArray *arr1;
@property(nonatomic,assign) int index1;

@end

(6)延迟载入,懒载入。仅仅有须要的时候才初始化载入数据。

也就是说,我们能够把数据型的属性的初始化放在这个数据的getter方法中。且做一个推断是否要又一次载入。

我们默认的arr1的getter方法是:

-(NSArray *)arr1{
    return _arr1;
}

改动为例如以下。即假设这个数组载入过数据,则不用反复载入,并且是用self.arr1调用到它的时候才载入,这就是延迟载入:

-(NSArray *)arr1{
    if (_arr1==nil) {//此处用_arr1而不用self.arr1是避免死循环。由于self.arr1也是调用这个函数。会一直循环调用自身
        NSMutableDictionary *dic1=[NSMutableDictionary dictionary];
        dic1[kICONKEY]=@"a.png";
        dic1[kDESCRIP]=@"This is the letter A";
        
        NSMutableDictionary *dic2=[NSMutableDictionary dictionary];
        dic2[kICONKEY]=@"b.png";
        dic2[kDESCRIP]=@"This is the letter B";
        
        NSMutableDictionary *dic3=[NSMutableDictionary dictionary];
        dic3[kICONKEY]=@"c.png";
        dic3[kDESCRIP]=@"This is the letter C";
        
        NSMutableDictionary *dic4=[NSMutableDictionary dictionary];
        dic4[kICONKEY]=@"d.png";
        dic4[kDESCRIP]=@"This is the letter D";
        
        NSMutableDictionary *dic5=[NSMutableDictionary dictionary];
        dic5[kICONKEY]=@"e.png";
        dic5[kDESCRIP]=@"This is the letter E";
        
        _arr1=[NSArray arrayWithObjects:dic1,dic2,dic3,dic4,dic5, nil];
    }
    
    return _arr1;
}

(7)再进一步:把数据独立存放在plist中,以后增减数据仅仅是改动plist文件,而不须要在代码中增减数据。

先创建plist文件:



然后,在代码中引用。凝视掉的那些数据都已经存放在plist文件里了。用最以下的几行来使用plist文件就可以,以后有增减数据,仅仅要修改plist文件,不须要修改代码:

-(NSArray *)arr1{
    if (_arr1==nil) {
//        NSMutableDictionary *dic1=[NSMutableDictionary dictionary];
//        dic1[kICONKEY]=@"a.png";
//        dic1[kDESCRIP]=@"This is the letter A";
//        
//        NSMutableDictionary *dic2=[NSMutableDictionary dictionary];
//        dic2[kICONKEY]=@"b.png";
//        dic2[kDESCRIP]=@"This is the letter B";
//        
//        NSMutableDictionary *dic3=[NSMutableDictionary dictionary];
//        dic3[kICONKEY]=@"c.png";
//        dic3[kDESCRIP]=@"This is the letter C";
//        
//        NSMutableDictionary *dic4=[NSMutableDictionary dictionary];
//        dic4[kICONKEY]=@"d.png";
//        dic4[kDESCRIP]=@"This is the letter D";
//        
//        NSMutableDictionary *dic5=[NSMutableDictionary dictionary];
//        dic5[kICONKEY]=@"e.png";
//        dic5[kDESCRIP]=@"This is the letter E";
        
//        _arr1=[NSArray arrayWithObjects:dic1,dic2,dic3,dic4,dic5, nil];
        
        //取得mainBundle。即程序主目录
        NSBundle *path=[NSBundle mainBundle];
        //用取得的mainBundle来查找文件,返回路径
        NSString *pathFile=[path pathForResource:@"imgdata" ofType:@"plist"];
        _arr1=[NSArray arrayWithContentsOfFile:pathFile];
    }
    
    return _arr1;
}

(8)补充:怎么查看这个mainBundle资源库?

NSBundle* path=[NSBundle mainBundle]就是拿到这个资源库的路径。返回的是NSBundle对象,事实上是一个路径,能够通过这个来訪问资源库里面的全部资源。事实上它详细放在哪里?无须上网查找,直接用NSLog(@"%@",path);把这个路径打印出来不就ok了嘛。

NSBundle </Users/Andy/Library/Developer/CoreSimulator/Devices/64EDA842-5B0C-448D-BF2B-B063D09B60CB/data/Containers/Bundle/Application/E0E6FE95-99D1-4F70-84CD-D73059EA71DF/hello.app> 

顺着上面这个路径就找到了这个hello.app包>>>右击显示包内容。

大功告成。


(9)图片的大小不一样,怎样是好?

一般我们会给UIImageView设定好固定的宽高,可是图片假设有大有小怎么办?须要用到调用“内容模式”contentMode这个属性。即调整UIImageView里面内容怎么缩放摆放的。

一般默认的是拉伸图片直至填满整个UIViewView。这样一般会改变图片的宽高比,使得图片变形。我们一般经常使用的时,在保持图片宽高比的情况下,尽可能的填充这个UIImageView就可以,这个属性以及值就是(以上代码为什么没有加入,由于我们做得时候就设定了图片都是100*100,UIImageView也是100*100,所以不须要用到这个属性):

self.imgView1.contentMode=UIViewContentModeScaleAspectFit;

(10)文字太多。自己主动换行怎么设置?

以描写叙述文字的descLabel为例。我们本案例中文字并非非常多,且给了这个descLabel宽度200,足够用。全部仅仅有一行。假设我们把宽度设置为100,就发现显示不下。最后有个...表示省略内容。设置多行,有个numberOfLine属性。你能够设置详细的2,3,4等行数数字。也能够直接用0,表示无所谓多少行。

须要注意的时,设置多行的时候,你的descLabel高度要足够,不然依旧是显示...省略号。我们原先是30的高度,此处改成了60。

    self.descLabel.frame=CGRectMake(110, 300, 100, 60);
    self.descLabel.numberOfLines=0;


总结:要自己动手。尽管都明确当中原理,可是真正操作起来,会遇到一些非常小可是非常重要的问题,一个一个的解决定,这种积累,预计新手和差异老兵。


版权声明:本文博客原创文章。博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/hrhguanli/p/4682625.html