当UIWindow RootViewController更改SWIFT时触发事件/方法 [英] Trigger an event / method when UIWindow RootViewController changes SWIFT

查看:335
本文介绍了当UIWindow RootViewController更改SWIFT时触发事件/方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发iOS框架.我要求用户进入或退出特定的View Controller时记录事件.为此,我在考虑是否可以在根视图控制器更改时注册通知以触发自定义方法.或者使用KVO.但是从实现的角度来看,我不知道如何执行此操作,因为我找不到任何此类通知.

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.

请注意,这是一个框架项目.因此,先构建框架,然后将其添加/嵌入到另一个应用程序中.我没有该应用程序中有关视图控制器的任何信息.我唯一可以访问的是UIWindow的根视图控制器.因此,我需要知道何时发生更改.

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.

推荐答案

使其正常运行.答案是方法混乱.不建议将其作为解决该问题的首选方法.但是,如果使用得当,并且您确切知道自己在做什么,那是必经之路.

Got it working. The answer was method swizzling. Not recommended as a first solution to the problem. But if used carefully and you know exactly what you are doing that it is the way to go.

在这里找到了一篇非常有用的文章:方法混乱

Found a very useful article here: Method Swizzling

这篇关于当UIWindow RootViewController更改SWIFT时触发事件/方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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