【问题标题】:iOS - Calling Selector on Button that has a ParameteriOS - 在具有参数的按钮上调用选择器
【发布时间】:2011-07-21 08:28:56
【问题描述】:

您好,希望这是一个容易解决的问题。

我正在尝试做的是在按钮操作上有一个选择器,该选择器使用要播放的电影的文件名调用我的其他方法。如果我将文件名移回该方法,它可以完美地工作。

我认为我遇到的问题是将选择器添加到按钮的正确方法。任何意见/建议或建议将不胜感激。

提前致谢。

    //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

【问题讨论】:

    标签: xcode ipad ios4 uibutton selector


    【解决方案1】:

    UIControl(UIButton 是其中之一)的操作可以具有三种不同的方法签名:

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

    所以你尝试做的事情是不可能的。您不能使用 UIControlEvent 发送自定义对象。

    也许这样的事情可以解决您的问题。

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

    当然你必须改变你错误的 addTarget 方法

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

    【讨论】:

      【解决方案2】:

      看看这个..

      - (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;
          }
      }
      

      【讨论】:

      • 什么?比较帧的 NSString 表示?那是一个新的。很有创意:-)
      【解决方案3】:

      好吧,您的问题是addTarget:action:withObject:forControlEvents: 不是UIControl 上存在的方法。您需要找到其他方法来跟踪您的对象值,无论是在 ivar、一些中间数据源还是其他东西中。

      前段时间有一个similar question 有一些建议。

      【讨论】:

      • 害怕是这样的……猜想这意味着我只是调用了另一个调用我想调用的方法的方法。
      【解决方案4】:

      你可以这样做:

      buttonInfo.tag = 1;
      

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

      ...

      【讨论】:

      • 这个方法对我不起作用..它说对成员标签的请求不是联合或结构的总和。
      【解决方案5】:
      [infobuttonTwo addTarget:self action:@selector(buttonInfo:)forControlEvents:UIControlEventTouchUpInside];
      

      【讨论】:

        【解决方案6】:

        我的解决方案是创建一个保存对象的全局变量。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-04-05
          • 2020-09-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-04-17
          • 1970-01-01
          相关资源
          最近更新 更多