美文网首页
iOS实战之查看ViewController启动顺序

iOS实战之查看ViewController启动顺序

作者: SuAdrenine | 来源:发表于2017-02-17 14:52 被阅读21次

    使用runtime特性新建一个类目,然后创建一个pch 文件,里面写上 #define LogController,将其写入DEBUG宏中,发布正式版本的时候,这些日志就不会打印了,这样就能看到APP启动之后ViewController的启动顺序了

    显示界面之后ViewController的启动顺序
    #import "UIViewController+Debug.h"
    #import <objc/objc-runtime.h>
    
    @implementation UIViewController (Debug)
    
    + (void)load
    {
    #ifdef LogController
        Method viewWillAppear = class_getInstanceMethod(self, @selector(viewWillAppear:));
        Method logViewWillAppear = class_getInstanceMethod(self, @selector(logViewWillAppear:));
        method_exchangeImplementations(viewWillAppear, logViewWillAppear);
    #endif
    }
    
    - (void)logViewWillAppear:(BOOL)animated
    {
        NSString *className = NSStringFromClass([self class]);
        if ([className containsString:@"Controller"]) {
            NSLog(@"----- %@ ----- is appearing",className);
        }
        [self logViewWillAppear:animated];
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:iOS实战之查看ViewController启动顺序

          本文链接:https://www.haomeiwen.com/subject/qsxlwttx.html