LittleFunny
LittleFunny

Reputation: 8375

Objective-c : addTarget to UIButton not calling

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

Upvotes: 0

Views: 3266

Answers (4)

KING
KING

Reputation: 980

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

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

Upvotes: 1

Nafiz
Nafiz

Reputation: 462

action:

@selector(buttonTouched:)

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

@selector(buttonTouched)

Upvotes: 0

Rui Peres
Rui Peres

Reputation: 25907

It's a bit strange it doesn't get called, since it seens to be set up correctly (https://stackoverflow.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).

Upvotes: 1

jxpx777
jxpx777

Reputation: 3641

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

Upvotes: -1

Related Questions