iOS 快速梳理代碼之頁(yè)面定位

到一個(gè)新公司,最艱難的時(shí)刻就是前一周,項(xiàng)目代碼不熟悉,人也都不認(rèn)識(shí),關(guān)鍵項(xiàng)目文檔還> 缺失,尼瑪,真是各種不爽


剛?cè)胄鹿緯r(shí),一般不可能對(duì)項(xiàng)目結(jié)構(gòu)快速掌握,假如任務(wù)是類(lèi)似修改頁(yè)面的bug,就找頁(yè)面對(duì)應(yīng)文件可能就很費(fèi)事,多個(gè)頁(yè)面的話(huà)簡(jiǎn)直能讓人找的上火。如果能迅速定位當(dāng)前頁(yè)面及相關(guān)跳轉(zhuǎn)頁(yè)面文件位置,那么問(wèn)題就能變得簡(jiǎn)單些。

  • 使用category動(dòng)態(tài)添加的方法,release時(shí)自動(dòng)關(guān)閉打印位置,對(duì)工程無(wú)影響
  • 一鍵導(dǎo)入,只需導(dǎo)入頭文件"UIViewController+SWIZZLocaltion.h",簡(jiǎn)單方便
  • 盡可能詳細(xì)的描述跳轉(zhuǎn)信息,有更詳盡的描述方案敬請(qǐng)留言

核心代碼片段

#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跳轉(zhuǎn)";
            if (self.presentingViewController) {
                logStr = @"present+Navi跳轉(zhuǎn)";
            }
            for (UIViewController *viewControl in self.navigationController.viewControllers) {
                logStr = [logStr stringByAppendingFormat:@"-->%@",[viewControl.class description]];
            }
        }else if(self.presentingViewController){//presentingViewController 上一視圖
            logStr = @"present跳轉(zhuǎn)-->";
            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:@"未知跳轉(zhuǎn)-->%@",[self.class description]];
        }
        NSLog(@"強(qiáng)大的黑魔法 %@",logStr);
    }
}

最后附上小demo:ViewControllerLocaltion

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,255評(píng)論 4 61
  • *面試心聲:其實(shí)這些題本人都沒(méi)怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個(gè)offer,總結(jié)起來(lái)就是把...
    Dove_iOS閱讀 27,217評(píng)論 30 472
  • 距離上周挨罵,剛剛七天。 誰(shuí)料想,一個(gè)周后的同一時(shí)間,我又被領(lǐng)導(dǎo)罵了一頓。 情緒沒(méi)有對(duì)錯(cuò) 是的,他的情緒一樣沒(méi)有對(duì)...
    竹筠益閱讀 700評(píng)論 2 0
  • 為什么說(shuō)明這個(gè)話(huà)題呢?因?yàn)槲倚娜缑麋R,通過(guò)觀察周邊人的問(wèn)題我就知道我有這個(gè)溝通的問(wèn)題,就算現(xiàn)在沒(méi)有,起碼之前有,以...
    際安閱讀 134評(píng)論 0 0
  • - [ ] 互惠:使用退讓互惠可以達(dá)成目的。先闡述大請(qǐng)求,不通過(guò),申請(qǐng)小請(qǐng)求,一般都會(huì)通過(guò)。 - [ ] 承諾一致...
    陌靄閱讀 164評(píng)論 0 1