カスタム クラスがあり、そのクラスにはUIButton
インスタンス変数があります。クラス指定イニシャライザにこのコードを追加しました。
theFishDeathView = [UIButton buttonWithType:UIButtonTypeCustom];
[theFishDeathView setFrame:CGRectMake(15, 15, 50, 50)];
[theFishDeathView setImage:[UIImage imageNamed:@"Small fish - death.png"] forState:UIControlStateNormal];
したがって、これはボタンを適切に割り当て/初期化する必要があります。これが呼び出されると、ボタンが画面に表示されます (もちろん、サブビューとして追加されます)。
ここで、オブジェクトでこのメソッドを呼び出します。
[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