A smart home in Shenzhen to develop the ipad version, the existing version of the iphone has been modified, the app's interface using Xib. Xib is fast and efficient compared to the code-to-write interface, but is poorly maintainable and readable. After seeing the code, I was in a very complicated mood.
Any programming area, anyone can participate, take iOS development, some people write code is able to execute, but not only requirements are able to execute, when the function of the project is constantly increasing, the untidy writing style will let people look to vomit, do not talk about maintainability, it is a mess.
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:@] Xrdimmingview Controller-6 "Bundle:nil"; } else {DIMMINGVC = [[Xrdimmingviewcontroller alloc]initwithnibname:@ "Xrdimmingviewcontroller" b Undle: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 ' b Undle:nil]; } else if (iphone6p) {CURTAINVC = [[Xrcurtainviewcontroller alloc]initwithnibname:@] Xrcurtainvie wcontroller-6p "Bundle:nil"; } else {CURTAINVC = [[Xrcurtainviewcontroller alloc]initwithnibname:@ "Xrcurtainviewcontroller" b Undle:nil]; } curtainvc.devicetype = Devtypeid; Curtainvc.curtaindeviceid = DevID; [Self.navigationcontroller PUSHVIEWCONTROLLER:CURTAINVC Animated:yes]; Return }
It is entirely possible to write a common method to determine the device type to load the corresponding xib.
-(NSString *) Selectviewcontrollerxibname: (NSString *) className { nsstring *str; if (iphone6p) { str = [ClassName stringbyappendingstring:@ " -6p"]; } else if (iPhone6) { str = [ClassName strin gbyappendingstring:@ "-6"]; } else { str = className; } return str;}
For example, the following code can be streamlined, so that the logic of the program is clear
Before streamlining: 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; }
After refinement: 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;}
[Original]ios simplifies redundant code