简体   繁体   English

objective-c / ios跟踪屏幕使用情况

[英]objective-c/ios track screen usage

Let's say, for instance, that I want to place some code in the viewDidAppear: method for all UIViewController (including subclasses) objects from my project: 比方说,我想在我的项目中为所有UIViewController (包括子类)对象的viewDidAppear:方法中放置一些代码:

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

    NSLog(@"Did show: %@:%@", NSStringFromClass([self class]), self.title);
}

Is there a way to do this without using categories (for UIViewController ), and without having to manually change the superclass of all my view controllers to a new class that defines this method? 有没有办法在不使用类别( UIViewController )的情况下执行此操作,而无需手动将所有视图控制器的超类更改为定义此方法的新类?
The reason I don't want to use a category is because I might want to define methods and call their super implementation. 我不想使用类别的原因是因为我可能想要定义方法并调用它们的超级实现。

For instance, is it possible to automatically add an intermediary class between UIViewController and whatever other classes inherit from UIViewController at runtime (or using preprocessor macros)? 例如,是否可以在UIViewController和在运行时(或使用预处理器宏)从UIViewController继承的任何其他类之间自动添加中间类?

The common way to achieve this is called method swizzling. 实现这一目标的常用方法称为方法调配。 Mike Ash has an article of how to do this correctly. Mike Ash有一篇关于如何正确执行此操作的文章

The general idea is to exchange the original implementation of an Objective-C method. 一般的想法是交换Objective-C方法的原始实现。 Usually you call through to the original implementation from the inserted function/method. 通常从插入的函数/方法调用原始实现。

Here's a quote from Mike's article: 以下是Mike的文章引用:

The Obligatory Warning 强制性警告

Overriding methods on classes you don't own is a dangerous business. 在您不拥有的类上覆盖方法是一项危险的业务。 Your override could cause problems by breaking the assumptions of the class in question. 您的覆盖可能会通过打破相关类的假设而导致问题。 Avoid it if it's at all possible. 如果可能的话,尽量避免使用它。 If you must do it, code your override with extreme care. 如果必须这样做,请特别小心地对覆盖进行编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM