iOS 快速梳理代码之页面定位

到一个新公司,最艰难的时刻就是前一周,项目代码不熟悉,人也都不认识,关键项目文档还> 缺失,尼玛,真是各种不爽


刚入新公司时,一般不可能对项目结构快速掌握,假如任务是类似修改页面的bug,就找页面对应文件可能就很费事,多个页面的话简直能让人找的上火。如果能迅速定位当前页面及相关跳转页面文件位置,那么问题就能变得简单些。

  • 使用category动态添加的方法,release时自动关闭打印位置,对工程无影响
  • 一键导入,只需导入头文件"UIViewController+SWIZZLocaltion.h",简单方便
  • 尽可能详细的描述跳转信息,有更详尽的描述方案敬请留言

核心代码片段

#ifdef DEBUG
+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method orginWillAppear = class_getInstanceMethod([self class], @selector(viewWillAppear:));
        Method swizWillAppear = class_getInstanceMethod([self class], @selector(location_viewWillAppear:));
        bool isAddWillAppear = class_addMethod([self class], @selector(viewWillAppear:), method_getImplementation(swizWillAppear), method_getTypeEncoding(swizWillAppear));
        if (isAddWillAppear) {
            class_replaceMethod([self class], @selector(location_viewWillAppear:), method_getImplementation(orginWillAppear), method_getTypeEncoding(orginWillAppear));
        } else {
            method_exchangeImplementations(orginWillAppear, swizWillAppear);
        }
    });
}
#endif
#pragma mark - SwizzMethods
- (void)printViewLocaltiionAndJumpRelation{
    if (![self isKindOfClass:[UITabBarController class]]&&![self isKindOfClass:[UINavigationController class]]) {
        NSString *logStr;
        if ([[self parentViewController] isKindOfClass:[UINavigationController class]]) {
            logStr = @"push跳转";
            if (self.presentingViewController) {
                logStr = @"present+Navi跳转";
            }
            for (UIViewController *viewControl in self.navigationController.viewControllers) {
                logStr = [logStr stringByAppendingFormat:@"-->%@",[viewControl.class description]];
            }
        }else if(self.presentingViewController){//presentingViewController 上一视图
            logStr = @"present跳转-->";
            if ([self.presentingViewController isKindOfClass:[UINavigationController class]]) {
                UINavigationController *navi = (UINavigationController *)self.presentingViewController;
                logStr = [logStr stringByAppendingFormat:@"%@-->%@",[[navi.viewControllers lastObject].class description],[self.class description]];
            }else{
                logStr = [logStr stringByAppendingFormat:@"%@-->%@",[self.presentingViewController.class description],[self.class description]];
            }
            
        }else{
            logStr = [NSString stringWithFormat:@"未知跳转-->%@",[self.class description]];
        }
        NSLog(@"强大的黑魔法 %@",logStr);
    }
}

最后附上小demo:ViewControllerLocaltion

你可能感兴趣的:(iOS 快速梳理代码之页面定位)