0
votes

I tried to use addTarget to add an event to a UIButton like this

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];

The function definition is this:

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

This is the code for EGOImageButton init function

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

}

Error is looking like this:

-[NSCFType buttonTouched:]: unrecognized selector sent to instance 0x6c804a0 2012-06-06 01:36:59.061 Photo Collage for Facebook[2416:16103] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType buttonTouched:]: unrecognized selector sent to instance 0x6c804a0' * Call stack at first throw: ( 0 CoreFoundation 0x0046a5a9 __exceptionPreprocess + 185 1 libobjc.A.dylib 0x01402313 objc_exception_throw + 44 2 CoreFoundation 0x0046c0bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 3 CoreFoundation 0x003db966 __forwarding + 966 4 CoreFoundation 0x003db522 _CF_forwarding_prep_0 + 50

4
what is EGOImageButton? an UIButton?Chakalaka
Yes it inherited from UIButton. It gave me EXC_BAD_ACCESS. I commented that addTarget line. It work without error. Sometime it gave me unrecognized selectorLittleFunny
where is buttonTouched defined?Chakalaka
if u import this EGOImageButton class then directly use this class method. directlyRinju Jain
I have placed an #import of that file. Otherwise I would have error when i start running the application.LittleFunny

4 Answers

1
votes

It's a bit strange it doesn't get called, since it seens to be set up correctly (https://stackguides.com/questions/8968446/cant-able-to-add-click-event-in-uibutton as a reference ). My first guess, without actually have access to your code, is that something is above your image and hence the click event is not passed to the UIButton sub-class (EGOImageButton).

1
votes

Check to make sure the method your selector is refered to contains a sender of type UIButton

so like this -(void)buttonTouched:(UIButton *)sender

0
votes

action:

@selector(buttonTouched:)

I think you don't need colon : here, remove it and try action: like this:

@selector(buttonTouched)
-1
votes

I think your buttonTouched method needs to have a return type of IBAction instead of void.