Preface:
- For a large project, the most annoying thing is that it is hard to find the corresponding viewController in many interfaces, and it takes a long time to find the corresponding class to change things.
- Especially when you take over a big project, you are not familiar with the overall business logic, the overall architecture system, so that you can fix the BUG of a page, it is estimated that you will have to look for the corresponding viewController of this page for a long time.
thinking
- Is there a way to get started on a big project quickly? Quickly find the viewController for a page, okay?
Train of thought
- As each page appears, it prints out which class is about to appear, as shown in the figure below
Snip20161001_4.png
The solution
- Plan 1
- Create a base viewController throughout the project, and then inherit all viewControllers in the project from the base viewController, and then override the viewWillAppear method in the base class
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSString *className = NSStringFromClass([self class]);
NSLog(@"%@ will appear", className);
}Copy the code
- Scheme 2
- Create a class for UIViewContoller and swap methods in the class, keeping the original method and printing information
// // UIViewController+Swizzling.m // CollectionsOfExample // // Created by MAC on 16/10/1. // Copyright © 2016年 chenfanfang. All rights reserved. // #import "UIViewController+Swizzling.h" #import @implementation UIViewController (Swizzling) + (void)load {// We only need to see which viewController will appear at development time so there is no need to swap methods in release mode #ifdef DEBUG ViewWillAppear Method viewWillAppear = class_getInstanceMethod(self, @selector(viewWillAppear:)); ViewWillAppear Method logViewWillAppear = class_getInstanceMethod(self, @selector(logViewWillAppear:)); Method_exchangeImplementations (viewWillAppear, logViewWillAppear); #endif } - (void)logViewWillAppear:(BOOL)animated { NSString *className = NSStringFromClass([self class]); // In this case, you can filter by specifying which viewControllers to print, If ([className hasPrefix:@"UI"] == NO) {NSLog(@"% @will appear",className); } / / the following method calls, is actually called viewWillAppear [self logViewWillAppear: animated]; } @endCopy the code
Advantages and disadvantages analysis
-
Scheme 1 is suitable for a new project, a project built from scratch, to create a base class Controller, this programming idea is very desirable. But for an already established project, scheme 1 won’t work. You can’t suggest a base class and then change all controller inherited classes to base classes, can you? It’s too much work, too much trouble.
-
Option 2 applies to both projects built from scratch and those already in place.