记录项目中所有UIViewControllers的类名 [英] Logging the class name of all UIViewControllers in a project

查看:309
本文介绍了记录项目中所有UIViewControllers的类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们收到了来自外包的巨大项目,我们正在努力修复。项目中有数百个视图控制器。我们的目标是轻松确定我们当前在设备上查看哪个类。

We have received a HUGE project from outsourcing that we are trying to "repair". There are hundreds of view controllers within the project. Our goal is to easily determine which class we are currently looking at on the device.

我们的解决方案(无效,因此SO问题)如下。

Our solution (which didn't work, hence the SO question) follows.

通过以下类别覆盖UIViewController的viewDidAppear方法:

Override the viewDidAppear method of UIViewController via a category with this:

-(void)viewDidAppear:(BOOL)animated
{
    NSLog(@"Current View Class: %@", NSStringFromClass(self.class));
    [self viewDidAppear:animated];
    //Also tried this:
    //[super viewDidAppear:animated];
}

此类别将放在 .pch 中该项目。

This category would be put in the .pch of the project.

这样就不需要在数百个视图控制器中放置额外的代码,并且可以轻松打开和关闭。它没有用,因为正如我们现在所了解的那样,< meme >不会简单地通过类别< /覆盖现有方法meme >。

This would require no extra code to be put in the hundreds of View Controllers and be easily turned on and off. It didn't work because, as we've learned now, <meme>one does not simply override an existing method via category</meme>.

我们缺少什么?!

推荐答案

答案是调整方法!以下是我们提出的结果:

The answer is to swizzle the methods! Here is what we came up with:

#import "UIViewController+Logging.h"
#import <objc/runtime.h>

@implementation UIViewController (Logging)

-(void)swizzled_viewDidAppear:(BOOL)animated
{
    NSLog(@"Current View Class: %@", NSStringFromClass(self.class));
    [self swizzled_viewDidAppear:animated];
}

+ (void)load
{
    Method original, swizzled;

    original = class_getInstanceMethod(self, @selector(viewDidAppear:));
    swizzled = class_getInstanceMethod(self, @selector(swizzled_viewDidAppear:));

    method_exchangeImplementations(original, swizzled);

}
@end

这篇关于记录项目中所有UIViewControllers的类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆