类别冲突 [英] Category conflicts
问题描述
最近的问题让我开始思考类别冲突,特别是涉及UIViewController.h
中的UIViewControllerRotation
类别.此类别包括shouldAutorotateToInterfaceOrientation:
.如果要覆盖此方法(通过类别)并使其对每个UIViewController
运行相同的代码(链接的文章正在尝试完成),那么他们将使用相同的方法来划分两个类别-我读过一些导致不确定的行为.
This recent question got me thinking about category conflicts, specifically involving the UIViewControllerRotation
category within UIViewController.h
. This category includes the shouldAutorotateToInterfaceOrientation:
. If one wanted to override this method (via a category) and make it run the same code for every UIViewController
(as the linked post is trying to accomplish) then they'd have two categories with the same method - something that I read leads to undefined behavior.
我尝试了一下,对于iOS 4.3中基于标准视图的应用程序,旋转逻辑回退到默认的仅纵向旋转,有效地忽略了类别.在我的UIViewController
子类中没有定义任何shouldAutorotateToInterfaceOrientation:
方法.有趣的是,当我确实定义了shouldAutorotateToInterfaceOrientation:
方法并简称为return [super shouldAutorotateToInterfaceOrientation:]
时,便调用了该类别.因此,这给我留下了两个问题:
I gave this a try and, for a standard view-based application in iOS 4.3, the rotation logic fell back to the default, portrait only rotation, effectively ignoring the category. This was with no shouldAutorotateToInterfaceOrientation:
method defined in my UIViewController
subclass. Interestingly, when I did define the shouldAutorotateToInterfaceOrientation:
method and simply called return [super shouldAutorotateToInterfaceOrientation:]
then the category was called. So this leaves me with two question:
- 当类别方法相互冲突时,所有赌注是否都(行为上)?
- 如果他们想重写带有继承的类别方法,这是不是很幸运?
任何反馈都非常感谢!谢谢.
Any feedback is much appreciated! Thanks.
推荐答案
如果您对类方法的实现有冲突,会发生什么情况仍未定义. UIViewController
提供了shouldAutorotateToInterfaceOrientation:
的默认实现,因此您不能通过类别附加自己的实现.
It is undefined what happens if you have conflicting implementations of a category method. UIViewController
provides a default implementation of shouldAutorotateToInterfaceOrientation:
, so you cannot attach your own implementation via a category.
但是,您可以劫持-[UIViewController shouldAutorotateToInterfaceOrientation:]
并插入自己的实现.我在通过method_exchangeImplementations()劫持中对此进行了讨论.
You may, however, hijack -[UIViewController shouldAutorotateToInterfaceOrientation:]
and insert your own implementation. I have a discussion on that in Hijacking with method_exchangeImplementations().
必须非常小心地使用它,并且取决于UIViewController
的某些实现细节,这些细节可能会发生变化.因此,对于大多数问题,我不建议使用此方法.通常,如果您想要一个特殊的旋转视图控制器",那么它就是子类的目的.您创建MYSpecialViewController
并从中子类化.使用劫持(或将其动态插入到对象模型中的任何其他机制)将影响系统中的每个视图控制器,包括Apple提供的对视图控制器可能会或可能不太满意的视图控制器.但是对于某些问题,这是一个非常有用的解决方案.
This must be used very carefully, and is dependent on certain implementation details of UIViewController
which might change. So I don't recommend this approach for most problems. Generally if you want a "special rotating view controller" that is what subclassing is for. You make your MYSpecialViewController
and you subclass from that. Using hijacking (or any other mechanism that inserts itself into the object model dynamically) will impact every view controller in the system, including Apple-provided ones who may or may not react well to it. But for certain problems, it is a very useful solution.
这篇关于类别冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!