0

我有一个带有 NSString 类型参数的方法 @selector。当我编译应用程序时出现此错误

No visible @interface for 'UIButton' declares the selector 'addTarget:action:
withObject:forControlEvents

该方法在头文件中声明。这是代码:

 -(void)loadDetailListViewController: (NSString *)nameTable{
      //......
  }

 - (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id 
  <MKAnnotation>)annotation
  {
        //.....

   UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

   [rightButton addTarget:self action: @selector(loadDetailListViewController:) 
     withObject:self.nameTable forControlEvents:UIControlEventTouchUpInside];
    //here the error


        //.....
   }

不知道括号对不对

4

6 回答 6

3

该方法不存在。这确实

[rightButton addTarget:self action: @selector(loadDetailListViewController:) forControlEvents:UIControlEventTouchUpInside];

如果您需要将字符串传递给该方法,我建议使用字符串属性对 UIButton 进行子类化,您可以在那里设置它。该按钮在选择器中作为参数传递,因此您可以从那里获取它和字符串:

- (void)loadDetailListViewController:(MyCustomButtonClass *)button {

    // access the string from the button

}
于 2012-11-07T16:09:09.973 回答
0

UIButton (UIControl) 没有像您使用的那样的签名。去掉withObject就好了。您可以考虑删除tableName参数,因为它是对象属性并且可以直接读取。

文件:

  • (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
于 2012-11-07T16:06:31.940 回答
0

试试这个

 -(void)loadDetailListViewController: (id)sender{
      //......
  }

 - (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id 
  <MKAnnotation>)annotation
  {
        //.....

   UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

   [rightButton addTarget:self action: @selector(loadDetailListViewController:) 
     forControlEvents:UIControlEventTouchUpInside];
    //here the error


        //.....
   }
于 2012-11-07T16:07:45.543 回答
0

错误说明编译器找不到'addTarget:action: withObject:forControlEvents可以在UIButton.

UIControl有一个方法addTarget:action:forControlEvents:,但没有一个包含“ withObject: ”的方法

于 2012-11-07T16:08:32.307 回答
0

没有这样的方法 addTarget:action: withObject: forControlEvents

采用:

[rightButton addTarget:self action: @selector(loadDetailListViewController:) forControlEvents:UIControlEventTouchUpInside];

您不需要将 self.nameTable 传递给此方法,您可以在 loadDetailListViewController 中访问它:就好了

于 2012-11-07T16:08:39.207 回答
0

没有像这样的方法:addTarget:(id)target action:(SEL)action withObject:(id) forControlEvents:(UIControlEvents)controlEvents

将其更改为:addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents

请参考:UIControl 类

于 2012-11-07T16:10:22.090 回答