2

I'm working on an iOS framework. I have a requirement to log events when user enters or exits a particular View Controller. For that I was thinking if somehow I could be able to register a notification to trigger a custom method when the root view controller changes. Or perhaps use KVO. But I don't understand how to do this from an implementation point of view since I cannot find any such notification.

Any help in this regard would be highly appreciated. Thanks.

Please note that this is a framework project. So the framework is built and then added/embedded into another app. I don't have any information about the view controllers in that app. The only thing I can access is UIWindow's root view controller. So, I need to know when a change occurs in it.

4

2 回答 2

2

得到它的工作。答案是方法混搭。不推荐作为该问题的第一个解决方案。但是,如果仔细使用并且您确切地知道自己在做什么,那就是要走的路。

在这里找到了一篇非常有用的文章:Method Swizzling

于 2016-03-24T12:16:32.970 回答
0

您可以覆盖UIViewController'sviewWillAppearviewWillDisappear知道何时将呈现/关闭视图控制器。

或者,您可以使用viewDidAppearviewDidDisappear

如果您想代表用户进行日志记录,那么您实际上有两个选择:

1.提供一个基础UIViewController子类供他们覆盖,在viewWillAppear/ viewWillDisappearmethods中实现所需的逻辑。

2.实现方便的方法(如logAppearEventlogDisappearEvent),以便他们在子类中自己手动调用UIViewController

于 2016-03-21T07:06:35.703 回答