我有一个自定义类,该类有一个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 文件。很明显,它是theFishDeathView
Fish 对象的一个实例成员。
#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