关于tabview cell中的事件无法传递到next response 的问题(from stackoverflow)

本文探讨了iOS中UIView触摸事件的传递机制,特别是如何在自定义视图MyView和其父ViewController之间正确转发触摸事件的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Apple is really funny. I mean, they say that this works:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch* touch = [touches anyObject];
    NSUInteger numTaps = [touch tapCount];
    if (numTaps < 2) {
        [self.nextResponder touchesBegan:touches withEvent:event];
   } else {
        [self handleDoubleTap:touch];
   }
}

I have a View Controller. Like you know, View Controllers inherit from UIResponder. That View Controller creates a MyView object that inherits from UIView, and adds it as a subview to it's own view.

So we have:

View Controller > has a View (automatically) > has a MyView (which is a UIView).

Now inside of MyView I put that code like above with a NSLog that prints "touched MyView". But I forward the event to the next responder, just like above. And inside the ViewController I have another touchesBegan method that just prints an NSLog a la "touched view controller".

Now guess what: When I touch the MyView, it prints "touched MyView". When I touch outside of MyView, which is the view of the VC then, I get a "touched view controller". So both work! But what doesn't work is forwarding the event. Because now, actually the next responder should be the view controller, since there's nothing else inbetween. But the event handling method of the VC is never called when I forward it.

%$&!§!!

Ideas, guys?

Figured out weird stuff MyView's next responder is the view of the view controller. That makes sense, because MyView is a subview of that. But I didn't modify this UIView from the view controller. it's nothing custom. And it doesn't implement any touch event handling. Shouldn't the message get passed on to the view controller? How could I just let it pass? If I remove the event handling code in MyView, then the event arrives nicely in the view controller.

share | edit
 
feedback

6 Answers

According to a similar question, your method should work.

This leads me to think that your view's nextResponder is not actually the ViewController, as you suspect.

I would add a quick NSLog in your forwarding code to check what your nextResponder really points to:

if (numTaps < 2) {
    NSLog(@"nextResponder = %@", self.nextResponder);
    [self.nextResponder touchesBegan:touches withEvent:event];
}

You can also change your other NSLog messages so that they output type and address information:

NSLog(@"touched %@", self);

touched <UIView 0x12345678>

This should get you started on diagnosing the problem.

share | edit
 
 



I know this post is old but i thought id share because I had a similar experience. I fixed it by setting the userInteractionEnabled to NO for the view of the view controller that was automatically created.

share | edit
 
feedback

Yes it should 100% work in common case. I've just tried with test project and everything seems to be ok.

I've created View-Based Application. Using Interface Builder put new UIView up to the present View. Then create new file with subclass of UIView and select just created class for my new view. (Interface Builder->Class identity->Class->MyViewClass)

Add touches handler functions both for MyViewClass and UIViewController.

//MyViewClass 

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

//ViewController 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {  
  NSLog(@"controller touches");
}

I see both NSLogs when press MyViewClass. Did you use Interface Builder and XIB file when loading your ViewController or set view programatically with loadView function?

share | edit
 
feedback

Alao,At a tabView,you need to use

[[self nextResponder] nextResponder]

because [self nextResponder]means a UIView, and [[self nextResponder] nextResponder] means a tabcontrol in a tabcontrol,now you can get the touch event

share | edit | flag
  add comment

today,I find a better method to fix this or the question like this In yours UITableViewCell,you can add a delegate to listen the touch event,like this:

@protocol imageViewTouchDelegate

-(void)selectedFacialView:(NSInteger)row item:(NSInteger)rowIndex;

@end

@property(nonatomic,assign)id<imageViewTouchDelegate>delegate;

Then in yours UITableViewController:your can done like this:

@interface pressionTable : UITableViewController<facialViewDelegate>

and in the .m file you can implemate this delegate interface,like:

-(void)selectedFacialView:(NSInteger)row item:(NSInteger)rowIndex{
//TO DO WHAT YOU WANT}

NOTE:when you init the cell,you must set the delegate,otherwise the delegate is invalid,you can do like this

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

pressionCell *cell = (pressionCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[pressionCell alloc]initWithFrame:CGRectMake(0, 0, 240, 40)];
}

cell.delegate = self;

}

Thanks All.

PS:I kown this DOC is old,but I still add my method to fix the question,when a people meet a same question,he will get my help,Thanks.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值