我有3个UIView,一个堆叠在另一个之上

UITableview
planeView
rootView

TableView在顶部,rootView在底部。 (rootView不可见,因为TableView位于其顶部)

我已经在rootView中实现了以下代码

/*code in rootView*/



- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {}

期望当最顶部的 View (即TableView)被触摸或移动时将调用这些函数,但是相反,没有一个函数被调用。

我还尝试将以下代码放入TableView中,以便调用rootView方法
 /*code in TableView so that the rootView methods are called(TableView is the subview of rootView)*/

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 {
[super touchesBegan:touches withEvent:event];
[self.superview touchesBegan:touches withEvent:event];
 }

正如预期的那样,但是问题在于TableView委托(delegate)像
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

不被称为。

有什么方法可以确保在TableView类(didSelectRow :)和rootView中的touchesBegan:,touchesMoved ..函数中实现的TableView委托(delegate)也相应地被调用?

即当我单击TableCell时,-> TableView中的(didSelectRow:atIndex)函数和-> rootView中的(touchesBegan和touchesEnd)方法都被调用。

最佳答案

UITableView的子类中,您应该具有如下的touch方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.nextResponder touchesBegan:touches withEvent:event];
    [super touchesBegan:touches withEvent:event];
}

此处的区别在于,您是将触摸传递给下一个响应者而不是 super View ,并且您是在将触摸传递给 super 之前进行此操作。

然后在planeView中,您需要像这样传递触摸:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.superview touchesBegan:touches withEvent:event];
}

请记住,这仍然可能无法完全按照您的预期工作。 UITableView在后台进行了很多响应链处理,以使其看起来像UITableView(实际上是 subview 的复杂集合)只是按钮或标签之类的另一种 View 。

关于uitableview - 堆叠UITableViews不会在其 View 下方传递触摸事件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8127721/

10-12 14:39