0

我尝试使用 addTarget 将事件添加到这样的 UIButton

EGOImageButton* image = [[EGOImageButton alloc] initWithPlaceholderImage:[UIImage imageNamed:@"screen.png"]];
    [image setImageURL:[NSURL URLWithString:thumbnail]];
    [image setFrame:CGRectMake(x, y, self.cellsize, self.cellsize)];
    [image setTag: [self.photoIDs count]-1];
    [image addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:image];
    [image release];

函数定义是这样的:

-(void)buttonTouched:(id)sender {
   NSLog(@"Thumbnail Clicked");
 }

这是 EGOImageButton 初始化函数的代码

- (id)initWithPlaceholderImage:(UIImage*)anImage {
return [self initWithPlaceholderImage:anImage delegate:nil];    }

- (id)initWithPlaceholderImage:(UIImage*)anImage delegate:(id<EGOImageButtonDelegate>)aDelegate {
if((self = [super initWithFrame:CGRectZero])) {
    self.placeholderImage = anImage;
    self.delegate = aDelegate;
    [self setImage:self.placeholderImage forState:UIControlStateNormal];
}

return self;

}

错误看起来像这样:

-[ NSCFType buttonTouched:]:无法识别的选择器发送到实例 0x6c804a0 2012-06-06 01:36:59.061 Facebook 的照片拼贴 [2416:16103] *由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[__NSCFType buttonTouched :]: 无法识别的选择器发送到实例 0x6c804a0' *第一次抛出调用堆栈:( 0 CoreFoundation 0x0046a5a9 __exceptionPreprocess + 185 1 libobjc.A.dylib 0x01402313 objc_exception_throw + 44 2 CoreFoundation 0x0046c0bb -[NSObject(NSObject) doesNotRecognizeFoundationSelector:] + 0x003db966 __转发+ 966 4 核心基础 0x003db522 _CF_forwarding_prep_0 + 50

4

4 回答 4

1

它没有被调用有点奇怪,因为它看起来设置正确(https://stackoverflow.com/questions/8968446/cant-able-to-add-click-event-in-uibutton 作为参考)。我的第一个猜测是,实际上无法访问您的代码,您的图像上方有一些东西,因此点击事件不会传递给 UIButton 子类(EGOImageButton)。

于 2012-06-05T13:21:14.400 回答
1

检查以确保您的选择器引用的方法包含 UIButton 类型的发送者

就像这样 -(void)buttonTouched:(UIButton *)sender

于 2012-06-07T06:04:50.153 回答
0

行动:

@selector(buttonTouched:)

我认为你不需要冒号:在这里,删除它并尝试操作:像这样:

@selector(buttonTouched)
于 2012-06-05T13:29:53.737 回答
-1

我认为你的buttonTouched方法需要有一个返回类型IBAction而不是void.

于 2012-06-05T13:12:53.387 回答