2012-06-21 17 views
10

में कार्रवाई करने के लिए तर्क पारित करने के लिए मैं नीचेकैसे UIGestureRecognizer initWithTarget कार्रवाई

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

- (void) processTap 
{ 
//do something 
} 

कोड का उपयोग कर रहा लेकिन मैं processTap कार्य करने के लिए एक डेटा भेजने की जरूरत है। क्या इसे करने का कोई तरीका है?

+0

मुझे लगता है कि यह आप बाहर की मदद करनी चाहिए: http: //stackoverflow.com/a/5035335/177136 – Ger

उत्तर

30

उदाहरण:

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

स्टैक ओवरफ़्लो आपका स्वागत है! हमेशा अपने कोड को स्पष्टीकरण प्रदान करने का प्रयास करें। –

+1

यदि आपको केवल 'UIView' की आवश्यकता है जिसमें इशारा हुआ, तो यह एक खराब दस्तावेज, अभी तक वैध उत्तर है। अन्यथा, आपको इस प्रतिक्रिया के साथ गठबंधन करना होगा: http://stackoverflow.com/questions/11594610/objective-c-storing-hidden-information-into-a-uiview @nielsbot – SwiftArchitect

+0

से यह टैगिंग उपयोगकर्ता डेटा को पार करने के लिए बेहद उपयोगी है - नहीं गन्दा स्थान गणना करने की जरूरत है ... – kfmfe04

3

वहाँ iOS - UITapGestureRecognizer - Selector with Arguments में एक समान प्रश्न के लिए एक अच्छा जवाब है, लेकिन अगर आप डेटा है कि आप को देखने के लिए संलग्न करने के लिए नहीं करना चाहती पास करना चाहते हैं, मैं एक दूसरे बनाने की सलाह देते फ़ंक्शन जिसकी आपको आवश्यक डेटा तक पहुंच है। उदाहरण के लिए:

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 
} 
संबंधित मुद्दे