1

I have a custom class, and that class has a UIButton instance variable. I have added this code in the class designated initializer:

theFishDeathView = [UIButton buttonWithType:UIButtonTypeCustom];
[theFishDeathView setFrame:CGRectMake(15, 15, 50, 50)];
[theFishDeathView setImage:[UIImage imageNamed:@"Small fish - death.png"] forState:UIControlStateNormal];

So this should properly allocate / initialize the button. And truly enough, the button get's displayed on the screen when this is called (and of course added as a subview).

Now, I call this method on my object:

[theFishDeathView addTarget:self action:@selector(sellFish) forControlEvents:UIControlEventTouchDown];

And here is the sellFish method:

-(void) sellFish {
    thePlayer.dollars += worthInDollars * 3;
    [theFishDeathView removeFromSuperview];
}

But when I try and press the button, it doesn't call that method. Am I missing something here?

For the sake of completeness, here is the Fish.h file. It is clear that theFishDeathView is an instance member of the Fish object.

#import <Foundation/Foundation.h>

@interface Fish : NSObject
{
    float cookingTime;
    float weight;
    int worthInDollars;
    NSString *name;
    NSArray *animaionImages;

    int fishMovementSpeed;
}

// Will be used to display
@property (nonatomic, retain) UIImageView *theFishImageView;
@property (nonatomic, retain) UIButton *theFishDeathView;

// Create setter / getter methods
@property (nonatomic, retain) NSString *name;
@property (readonly) int worthInDollars;
@property (readonly) int fishMovementSpeed;

-(id) initWith: (NSString *)theName andWeight: (float)theWeight andCookingTime: (float)theCookingTime andValue: (int)theValue andMovementSpeed: (int)speed;

-(CGRect) newFrameWithWidth:(int)width andHeight:(int)height;

-(void) killFish;

// Cooking methods
-(void) startCooking;
-(void) isDoneCooking;
-(void) isOverCooked;
-(void) sellFish;

@end
5
  • Please include the sellFish method you are trying to call. Is it sellFish or sellFish: ?
    – jrturton
    Jan 22, 2012 at 10:15
  • I added the sellFish method, and it takes no arguments so it should be fine as sellFish
    – Seerex
    Jan 22, 2012 at 10:30
  • Please note - theFishDeathView, is a UIButton that is pointed to by a Fish *object, so do i have to change the addTarget: self? i tried changing it to nil but that didn't solve it, so i was wondering if there is something else to try there?
    – Seerex
    Jan 22, 2012 at 10:34
  • [theFishDeathView addTarget:self action:@selector(sellFish) forControlEvents:UIControlEventTouchDown]; check by debugging if this line is executed or not?
    – Shubhank
    Jan 22, 2012 at 10:58
  • it is being executed. I was wondering if it has anything to do with specifying self as the target? coz i mean, theFishDeathView (The UIBUtton *) is an instance member of the Fish class. And the method is defined in that Fish class, but i just assumed i should say self and it would look for the method in the class that the UIButton * is located in?
    – Seerex
    Jan 22, 2012 at 11:01

3 Answers 3

1

try

 -(void) sellFish:(id)sender

and (with the : after sellFish)

[theFishDeathView addTarget:self
                 action:@selector(sellFish:)
       forControlEvents:UIControlEventTouchUpInside];
1
  • Thanks, but that doesn't solve it either :( i've also tried saying theFishDeathView.userInteractionEnabled = YES but still nothing :(
    – Seerex
    Jan 22, 2012 at 16:04
0
  [theFishDeathView addTarget:self
                 action:@selector(sellFish)
       forControlEvents:UIControlEventTouchUpInside];

you wrote UIControlEventTouchDown not UIControlEventTouchDown

2
  • 3
    Huh, not sure i understand there mate? I used UIControlEventTouchDown not UIControlEventTouchDown? :=)
    – Seerex
    Jan 22, 2012 at 10:10
  • UIControlEventTouchUpInside is the standard event to use for buttons - code sample in the answer is OK, but the explanation is wrong.
    – jrturton
    Jan 22, 2012 at 10:14
0

I wanted people to know i found the error (with some help from the Apple developers forum) - it was a memory leak. I was trying to send a message to a zombie object (i.e a deallocated object). I thought it was retained by adding it as a subview, but i totally forgot it was the BUTTON i added as a subview, and NOT the class that contained the button. So the class itself got deallocated, and the button was retained, he's the reason why i could still press it.

For others dealing with similar issues, turn on the Zombie Objects Enabled thing in the plist.info file. That way, you will get an error message like this: "Trying to send action to deallocated object".

Thanks for trying to help me out :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.