标签:
正在给深圳某家智能家居开发iPad版本,在已经存在的iPhone版上修改,该app的界面采用的是xib。xib相比代码来写界面,快速高效,但是可维护性和可读性太差。言归正传,看到这些代码后,我的心情很复杂。
任何一门编程领域,任何人都可以参与,就拿iOS开发来说,有的人写出的代码是能执行,但不仅仅要求是能执行,当项目的功能不断增加时,不整洁的编写风格会让人看着想吐,就不用谈什么可维护性了,简直是一团糟。
else if (devTypeID==13||devTypeID==14) { XRDimmingViewController *dimmingVC; if (iphone6P) { dimmingVC = [[XRDimmingViewController alloc]initWithNibName:@"XRDimmingViewController-6P" bundle:nil]; } else if (iphone6) { dimmingVC = [[XRDimmingViewController alloc]initWithNibName:@"XRDimmingViewController-6" bundle:nil]; } else { dimmingVC = [[XRDimmingViewController alloc]initWithNibName:@"XRDimmingViewController" bundle:nil]; } dimmingVC.devTypeID = devTypeID; dimmingVC.devID = devID; [self.navigationController pushViewController:dimmingVC animated:YES]; return; } else if (devTypeID==15) { XRColorViewController *colorVC = [[XRColorViewController alloc] init]; colorVC.devTypeID = devTypeID; colorVC.devID = devID; [self.navigationController pushViewController:colorVC animated:YES]; return; } else if (devTypeID>=31&&devTypeID<=34) { XRCurtainViewController *curtainVC; if(iphone6) { curtainVC = [[XRCurtainViewController alloc]initWithNibName:@"XRCurtainViewController-6" bundle:nil]; } else if (iphone6P) { curtainVC = [[XRCurtainViewController alloc]initWithNibName:@"XRCurtainViewController-6P" bundle:nil]; } else { curtainVC = [[XRCurtainViewController alloc]initWithNibName:@"XRCurtainViewController" bundle:nil]; } curtainVC.deviceType = devTypeID; curtainVC.curtainDeviceID = devID; [self.navigationController pushViewController:curtainVC animated:YES]; return; }
完全可以写一个公用方法判断设备类型来加载对应的xib。
- (NSString *)selectViewControllerXibName:(NSString *)className { NSString *str; if (iPhone6p) { str = [className stringByAppendingString:@"-6p"]; } else if (iPhone6) { str = [className stringByAppendingString:@"-6"]; } else { str = className; } return str; }
比如下面的这段代码就可以精简了,让程序的逻辑清晰
// 精简前: else if (devTypeID==13||devTypeID==14) { XRDimmingViewController *dimmingVC; if (iphone6P) { dimmingVC = [[XRDimmingViewController alloc]initWithNibName:@"XRDimmingViewController-6P" bundle:nil]; } else if (iphone6) { dimmingVC = [[XRDimmingViewController alloc]initWithNibName:@"XRDimmingViewController-6" bundle:nil]; } else { dimmingVC = [[XRDimmingViewController alloc]initWithNibName:@"XRDimmingViewController" bundle:nil]; } dimmingVC.devTypeID = devTypeID; dimmingVC.devID = devID; [self.navigationController pushViewController:dimmingVC animated:YES]; return; }
// 精简后: else if (devTypeID==13||devTypeID==14) { XRDimmingViewController *dimmingVC = [[XRDimmingViewController alloc]initWithNibName:[self selectViewControllerXibName:NSStringFromClass([XRDimmingViewController class])] bundle:nil]; dimmingVC.devTypeID = devTypeID; dimmingVC.devID = devID; [self.navigationController pushViewController:dimmingVC animated:YES]; return; }
标签:
原文地址:http://www.cnblogs.com/sunmm/p/4272732.html