2012-03-02 61 views
0

我有UITableViewCells每个与几个UIImageViews。我想将事件传递给父UITableViewController,以便它可以对其执行操作。我怎样才能发回信息给UITableViewController让它知道哪个UIImageView触发了这个事件。使用下面的代码,看起来UITableViewController touchesEnded在任何子UITableViewCells触发事件时触发。任何方式在UIEvent中传递信息?如何识别哪个事件通过nextResponder传递给父项?

有没有更好的方法去处理事件?

//UITableViewCell 
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 

    if ([touch view] == imageView) 
    { 
     [[self nextResponder] touchesEnded:touches withEvent:event]; 
    } 
} 

//UITableViewController 
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 

    NSLog(@"clicked"); 
} 

回答

0

对于一个快速和肮脏的方式,你可以使用objc_setAssociatedObject和ObjC运行时objc_getAssociatedObject

#import <objc/runtime.h> 

设置对象:

// static char key; 
objc_setAssociatedObject(event, &key, self, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 

,并获得对象:

id yourInterestObj = objc_getAssociatedObject(event, &key); 

但我不推荐这种方式,它可能打破了MVC。我认为你应该使用Responder Chain patter来处理这种情况,请检查文档-sendAction:to:from:forEvent:UIApplication

+0

谢谢,我也能够通过使用代表实现目标。我建立了一个委托协议,并且工作得很好(还在发送者对象中存储了标识符数据)。 – Ryan 2012-03-06 22:28:04

相关问题