Objective-c:addTarget到UIButton没有调用

时间:2012-06-05 13:10:05

标签: objective-c ios events uibutton

我尝试使用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)doesNotRecognizeSelector:] + 187       3 CoreFoundation 0x003db966 __ 转发 + 966       4 CoreFoundation 0x003db522 _CF_forwarding_prep_0 + 50

4 个答案:

答案 0 :(得分:1)

它有点奇怪,它没有被调用,因为它可以正确设置(https://stackoverflow.com/questions/8968446/cant-able-to-add-click-event-in-uibutton as a reference)。我的第一个猜测,实际上没有访问你的代码,是因为某些东西在你的图像之上,因此click事件不会传递给UIButton子类(EGOImageButton)。

答案 1 :(得分:1)

检查以确保您所选择的方法包含UIButton类型的发件人

所以这样 - (void)按钮触摸:(UIButton *)发件人

答案 2 :(得分:0)

动作:

@selector(buttonTouched:)

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

@selector(buttonTouched)

答案 3 :(得分:-1)

我认为您的buttonTouched方法需要返回IBAction而不是void