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 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,255评论 4 61
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,217评论 30 472
  • 距离上周挨骂,刚刚七天。 谁料想,一个周后的同一时间,我又被领导骂了一顿。 情绪没有对错 是的,他的情绪一样没有对...
    竹筠益阅读 700评论 2 0
  • 为什么说明这个话题呢?因为我心如明镜,通过观察周边人的问题我就知道我有这个沟通的问题,就算现在没有,起码之前有,以...
    际安阅读 134评论 0 0
  • - [ ] 互惠:使用退让互惠可以达成目的。先阐述大请求,不通过,申请小请求,一般都会通过。 - [ ] 承诺一致...
    陌霭阅读 164评论 0 1