(iphone) [super TouchsBegan/Moved/Ended] 是做什么的?
大多数重载方法需要 [super theMethod]
调用。
(例如,[super viewDidLoad];
、[super viewWillAppear];
和 [super dealloc];
)
我没有多想是否我是否需要 [super TouchsBegan:withEvent:] 调用,但它似乎以某种方式发挥了作用。
我什么时候需要它,什么时候不需要它?
我正在尝试在需要时以编程方式取消触摸事件,这似乎与我提出的问题有关。
Most overloaded methods require [super theMethod]
call.
(For example, [super viewDidLoad];
, [super viewWillAppear];
and [super dealloc];
)
I didn't think twice whether I need [super touchesBegan:withEvent:]
call or not, but it seems to play a role somehow.
When do I need it and when don't I need it?
I'm trying to programmatically cancel touch events when I need to, and it seems to be related to the question I asked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然这一切都取决于对象的预期行为,但让我们指出基本的:如果您希望超类实现默认行为,则可以在重写方法中调用超类实现(您的意思是重写,而不是重载)。
所以你应该问的问题是:
1)超类实现是做什么的?
2) 我的物品需要它吗?
那么你的超类的
touchesBegan:withEvent:
等是做什么的?这取决于您的类层次结构。类层次结构中的自定义类可能已经覆盖了这些方法,并且您可能需要或不需要这些行为。我假设还没有自定义类覆盖这些方法,因此您只需检查 Cocoa 实现。touchesBegan:withEvent:
首先在 UIResponder 类中定义。让我们看一下 UIResponder 文档 ,其中说所以你在这里得到了一个理论。如果您的类直接继承自 UIResponder,则根本不需要调用超类实现,因为它什么也不做。如果它继承自其他 UIKit 子类(例如 UIView),则调用超类实现将启用在响应者链上转发消息。但是,重写
touchesBegan:withEvent:
可能意味着您的子类想要自行处理触摸事件,因此您不希望将事件沿着响应者链转发。所以,我猜你不想在这里调用超类实现。这确实取决于具体情况。假设您对 UIScrollView 进行了子类化,并实现了
touches...
方法之一。如果您仍然希望视图在用户拖动时滚动,那么您需要调用超类实现,因为它将为您处理滚动。总而言之,您必须能够回答以上两个问题才能做出决定。要了解超类实现的作用,请阅读Apple 文档。在许多情况下,如果超类实现所做的事情对于常见的预期行为至关重要,则指南文档会给出调用超类实现的明确建议。
While it all depends on the expected behaviors of the object, let's point out the basic: You call the superclass implementation in an overridden method (you mean overriding, not overloading) if you want default behaviors implemented by the superclass.
So the questions you should ask are:
1) What does the superclass implementation do?
2) Do I need it for my object?
So what does
touchesBegan:withEvent:
etc. of your superclass do? It depends on your class hierarchy. The custom classes in your class hierarchy may already override those methods and you may want those behaviors, or not. I assume that no custom class has yet overridden those methods, so you have to check Cocoa implementations only.touchesBegan:withEvent:
is first defined in UIResponder class. Let's look at the UIResponder documentation, which saysSo you get a theory here. If your class directly inherits from UIResponder, you don't need to call the superclass implementation at all as it does nothing anyway. If it inherits from other UIKit subclasses such as UIView, calling the superclass implementation will enable forwarding messages up the responder chain. However, overriding
touchesBegan:withEvent:
perhaps means that your subclass wants to handle touch events itself, so you don't want to forward the events up the responder chain. So, I guess you don't want to call the superclass implementation here.It really depends on the situation. Say, that you subclass a UIScrollView, and implements one of the
touches...
method. If you still want the view to scroll as the user drags, then you want to call the superclass implementation because it will handle scrolling for you.To sum up, you have to be able to answer the two questions above to decide. To find out what a superclass implementation does, read about it in the Apple Documentation. In many cases, if what a superclass implementation does is essential for common expected behaviors, the guide documents give an explicit advice to call the superclass implementation.