2012-06-21 15 views

risposta

30

Esempio:

UIImageView *myPhoto = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"tab-me-plese.png"]]; 
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processTap:)]; 

[myPhoto setTag:1]; //set tag value 
[myPhoto addGestureRecognizer:tap]; 
[myPhoto setUserInteractionEnabled:YES]; 

- (void)processTap:(UIGestureRecognizer *)sender 
{ 
    NSLog(@"tabbed!!"); 
    NSLog(@"%d", sender.view.tag);  
} 
+4

Benvenuti a Stack Overflow! Cerca sempre di fornire una spiegazione del tuo codice. –

+1

Se tutto ciò che serve è il 'UIView' in cui si è verificato il gesto, allora questa è una risposta scarsamente documentata, ma valida. In caso contrario, è necessario combinare questa risposta: http://stackoverflow.com/questions/11594610/objective-c-storing-hidden-information-into-a-uiview da @nielsbot – SwiftArchitect

+0

Questa codifica è estremamente utile per il passaggio di UserData - no bisogno di fare calcoli sulla posizione disordinati ... – kfmfe04

3

C'è una buona risposta ad una domanda simile a iOS - UITapGestureRecognizer - Selector with Arguments, ma se si vuole passare i dati che non si desidera allegare alla vista, si raccomanda di creare un secondo funzione che ha accesso ai dati di cui hai bisogno. Per esempio:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(passDataToProcessTap)]; 

- (void)passDataToProcessTap { 
    [self processTapWithArgument:self.infoToPass]; 
    // Another option is to use a static variable, 
    // Or if it's not dynamic data, you can just hard code it 
} 

- (void) processTapWithArgument:(id)argument { 
    //do something 
} 
Problemi correlati