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



8
down vote favorite
4

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

active oldest votes
up vote 2 down vote

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

This should get you started on diagnosing the problem.

share | edit
 
 



up vote 1 down vote

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
up vote 1 down vote

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

up vote 0 down vote

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
up vote 0 down vote

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)iddelegate;

Then in yours UITableViewController:your can done like this:

@interface pressionTable : UITableViewController

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.

你可能感兴趣的:(关于tabview cell中的事件无法传递到next response 的问题(from stackoverflow))