2011-03-23 12 views
8

हैलो यह आशा है कि हल करने के लिए यह एक आसान सवाल है।आईओएस - बटन पर कॉलर चयनकर्ता जिसमें पैरामीटर

जो मैं करने का प्रयास कर रहा हूं उसके पास एक बटन एक्शन पर एक चयनकर्ता है जो फ़ाइल के साथ मेरी दूसरी विधि को कॉल करने के लिए कॉल करता है। अगर मैं फ़ाइल नाम को विधि में वापस ले जाता हूं तो यह बेकार ढंग से काम करता है।

मुझे विश्वास है कि मैं स्टॉलिंग कर रहा हूं बटन के चयनकर्ता को जोड़ने का सही तरीका है। किसी भी इनपुट/सलाह या सिफारिशों की सराहना की जाएगी।

अग्रिम धन्यवाद।

- (void)action 
- (void)action:(id)sender 
- (void)action:(id)sender event:(UIEvent *)event 

इतना है कि आप क्या करने की कोशिश संभव नहीं है:

एक UIControl (UIButton एक है) के
//The Button 
    infobuttonTwo   = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    infobuttonTwo.frame  = CGRectMake(405, 204, 49, 58); 
    [infobuttonTwo addTarget:self 
         action:@selector(buttonInfo:) withObject:@"VTS_02_1" 
      forControlEvents:UIControlEventTouchUpInside]; 



//The Method It's calling 
-(void)buttonInfo:(NSString *) fileName { 



//The Error it's showing. 
-[UIRoundedRectButton addTarget:action:withObject:forControlEvents:]: unrecognized selector sent to instance 0x660e9e0 
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIRoundedRectButton addTarget:action:withObject:forControlEvents:]: unrecognized selector sent to instance 0x660e9e0' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x011bfbe9 __exceptionPreprocess + 185 
    1 libobjc.A.dylib      0x013145c2 objc_exception_throw + 47 
    2 CoreFoundation      0x011c16fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 
    3 CoreFoundation      0x01131366 ___forwarding___ + 966 
    4 CoreFoundation      0x01130f22 _CF_forwarding_prep_0 + 50 
    5 bCIT        0x000054f3 -[TrailersViewController popButtons] + 647 
    6 bCIT        0x0000444b -[TrailersViewController right] + 437 
    7 bCIT        0x00003b2d -[TrailersViewController handleSwipeFrom:] + 191 
    8 UIKit        0x005559c7 -[UIGestureRecognizer _updateGestureWithEvent:] + 727 
    9 UIKit        0x005519d6 -[UIGestureRecognizer _delayedUpdateGesture] + 47 
    10 UIKit        0x00557fa5 _UIGestureRecognizerUpdateObserver + 584 
    11 UIKit        0x0055818a _UIGestureRecognizerUpdateGesturesFromSendEvent + 51 
    12 UIKit        0x002f36b4 -[UIWindow _sendGesturesForEvent:] + 1292 
    13 UIKit        0x002eef87 -[UIWindow sendEvent:] + 105 
    14 UIKit        0x002d237a -[UIApplication sendEvent:] + 447 
    15 UIKit        0x002d7732 _UIApplicationHandleEvent + 7576 
    16 GraphicsServices     0x01af5a36 PurpleEventCallback + 1550 
    17 CoreFoundation      0x011a1064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52 
    18 CoreFoundation      0x011016f7 __CFRunLoopDoSource1 + 215 
    19 CoreFoundation      0x010fe983 __CFRunLoopRun + 979 
    20 CoreFoundation      0x010fe240 CFRunLoopRunSpecific + 208 
    21 CoreFoundation      0x010fe161 CFRunLoopRunInMode + 97 
    22 GraphicsServices     0x01af4268 GSEventRunModal + 217 
    23 GraphicsServices     0x01af432d GSEventRun + 115 
    24 UIKit        0x002db42e UIApplicationMain + 1160 
    25 bCIT        0x000029b4 main + 102 
    26 bCIT        0x00002945 start + 53 
) 
terminate called after throwing an instance of 'NSException 

उत्तर

4

खैर है, तो आपकी समस्या यह है कि addTarget:action:withObject:forControlEvents: एक विधि है कि UIControl पर मौजूद नहीं है। आपको अपने ऑब्जेक्ट वैल्यू को ट्रैक करने के लिए किसी अन्य तरीके की आवश्यकता है, या तो ivar, कुछ मध्यवर्ती डेटा स्रोत, या कुछ और।

कुछ समय पहले similar question था जिसमें कुछ सुझाव दिए गए थे।

+0

डर था यह कुछ इस तरह था ... लगता है कि इसका मतलब है कि मैं सिर्फ एक और तरीका है कि विधि मैं कॉल करना चाहते हैं कॉल कहते हैं। – Mytheral

17

कार्यों तीन अलग-अलग विधि हस्ताक्षर हो सकता है। आप अपने UIControlEvent के साथ एक कस्टम ऑब्जेक्ट नहीं भेज सकते हैं।

शायद ऐसा कुछ आपकी समस्या का समाधान है।

-(void)buttonInfo:(id)sender { 
    NSString *fileName = nil; 
    if (sender == infobuttonTwo) { 
     fileName = @"VTS_02_1"; 
    } 
} 

और निश्चित रूप से आप अपने गलत addTarget प्रक्रिया में परिवर्तन करना

[infobuttonTwo addTarget:self action:@selector(buttonInfo:) forControlEvents:UIControlEventTouchUpInside]; 
4

आप ऐसा कर सकते हैं:

buttonInfo.tag = 1; 

और

-(void)buttonInfo:(id)sender { 
     if(sender.tag == 1){ 
     NSString filename = @"VTS_02_1"; 
     } 
    } 

में ...

+0

यह मेथोस मेरे लिए काम नहीं कर रहा है .. memeber के लिए अनुरोध कह रहा है TAG टैग यूनियन या स्ट्रंक्चर नहीं है। – Christina

0
[infobuttonTwo addTarget:self action:@selector(buttonInfo:)forControlEvents:UIControlEventTouchUpInside]; 
-2

उस के लिए मेरे समाधान एक वैश्विक चर है कि वस्तु का आयोजन करेगा बनाने के लिए है ।

5

इस चेक बाहर ..

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    // Do any additional setup after loading the view, typically from a nib. 
    int y = 100; 

    for (int i = 0; i <= 5; i++) { 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [button addTarget:self action:@selector(aMethod:) 
        forControlEvents:UIControlEventTouchDown]; 
     [button setTitle:@"Show View" forState:UIControlStateNormal]; 
     button.frame = CGRectMake(80, y, 160, 40); 
     [button setTitle:[NSString stringWithFormat:@"Button-%i", i] 
              forState:UIControlStateNormal]; 
     [self.view addSubview:button]; 
     y = y + 60; 
    } 
} 

- (void)aMethod:(id)sender { 

    UIButton *button = sender; 

    int y = 100; 
    int i = 5; 

    for (int x = 0; x <= i; x++) { 
     NSString *frameString = 
      [NSString stringWithFormat:@"{{80, %i}, {160, 40}}", y]; 

     if ([NSStringFromCGRect(button.frame) isEqualToString:frameString]) { 
      NSLog(@"button %i clicked..", x); 
     } 
     y = y + 60; 
    } 
} 
+0

उपरोक्त कोड अच्छी तरह से काम करता है।, – Sri

+0

वाट? फ्रेम के एनएसएसटींग प्रस्तुतियों की तुलना करना? यह एक नया है। बहुत रचनात्मक :-) –

संबंधित मुद्दे