1

我有一个自定义类,该类有一个UIButton实例变量。我在类指定的初始化程序中添加了这段代码:

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

所以这应该正确分配/初始化按钮。确实,当它被调用时,按钮 get 会显示在屏幕上(当然也可以作为子视图添加)。

现在,我在我的对象上调用这个方法:

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

这是sellFish方法:

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

但是当我尝试按下按钮时,它不会调用该方法。我在这里错过了什么吗?

为了完整起见,这里是 Fish.h 文件。很明显,它是theFishDeathViewFish 对象的一个​​实例成员。

#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
4

3 回答 3

1

尝试

 -(void) sellFish:(id)sender

和(与:售后鱼)

[theFishDeathView addTarget:self
                 action:@selector(sellFish:)
       forControlEvents:UIControlEventTouchUpInside];
于 2012-01-22T14:08:57.953 回答
0
  [theFishDeathView addTarget:self
                 action:@selector(sellFish)
       forControlEvents:UIControlEventTouchUpInside];

UIControlEventTouchDown没有写UIControlEventTouchDown

于 2012-01-22T09:58:15.413 回答
0

我想让人们知道我发现了错误(在 Apple 开发者论坛的帮助下)——这是内存泄漏。我试图向僵尸对象(即已释放的对象)发送消息。我认为它是通过将其添加为子视图来保留的,但我完全忘记了它是我作为子视图添加的 BUTTON,而不是包含该按钮的类。所以类本身被释放,按钮被保留,他是我仍然可以按下它的原因。

对于其他处理类似问题的人,请打开 plist.info 文件中的 Zombie Objects Enabled 事物。这样,您将收到如下错误消息:“尝试向已释放对象发送操作”。

感谢您尝试帮助我:)

于 2012-01-25T07:30:24.453 回答