如何跟踪UIView从没有子类的情况下从超级视图中移除?(How to track when a UIView is removed from superview without subclassin

编程入门 行业动态 更新时间:2024-10-28 05:20:15
如何跟踪UIView从没有子类的情况下从超级视图中移除?(How to track when a UIView is removed from superview without subclassing?)

我想在我的应用程序中创建一个类,用于跟踪注册的UIView何时从屏幕上移除。 例如,这就像调用一个[MyViewTracker trackView:view]并等待类似委托调用或删除时的通知一样。

由于我不拥有视图,因此我无法将它们view.superview以通知我,也无法使用KVO在view.superview或view.window跟踪更改。 我可以不时检查正在跟踪的视图是否仍在显示,但它看起来不是一个好的解决方案。

是否还有其他解决方案可以完成? 也许在UIView的工作方式上出现混乱?

任何帮助,将不胜感激。

谢谢

I want to make a class in my app that keeps track of when a registered UIView is removed from the screen. For instance, it would be like calling a [MyViewTracker trackView:view] and waiting for something like a delegate call or a notification of when it is removed.

Since I don't own the views, I can't subclass them to notify me, nor I was able to use KVO to track changes at view.superview or view.window. I could check from time to time if the views that are being tracked are still being showed, but it doesn't look like a good solution.

Are there any other solutions that could be done? Maybe swizzling how a UIView works?

Any help would be appreciated.

Thanks

最满意答案

它看起来真的很黑,应该避免,但你可以调用removeFromSuperview ,调用你的跟踪方法并回调原始实现。 那是如果你真的需要。 这里有一篇很棒的文章: https : //www.mikeash.com/pyblog/friday-qa-2010-01-29-method-replacement-for-fun-and-profit.html 。

It really seems hacky and something you should avoid, but you could swizzle removeFromSuperview, call your tracking method and call back the original implementation. That is if you really need to. There is an excellent article about swizzling here: https://www.mikeash.com/pyblog/friday-qa-2010-01-29-method-replacement-for-fun-and-profit.html.

更多推荐

本文发布于:2023-07-31 00:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340416.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子类   视图   移除   情况下   UIView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!