1

カスタム クラスがあり、そのクラスには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
4

3 に答える 3

1

try

 -(void) sellFish:(id)sender

and (with the : after sellFish)

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

私は(Appleデベロッパフォーラムの助けを借りて)エラーを見つけたことを人々に知ってもらいたかった-それはメモリリークだった。ゾンビオブジェクト(つまり、割り当てが解除されたオブジェクト)にメッセージを送信しようとしていました。サブビューとして追加することで保持されていると思いましたが、ボタンを含むクラスではなく、サブビューとして追加したボタンであることを完全に忘れていました。そのため、クラス自体の割り当てが解除され、ボタンが保持されました。それが、私がまだそれを押すことができた理由です。

同様の問題を扱っている他の人のために、plist.infoファイルでゾンビオブジェクトを有効にすることをオンにします。そうすると、「割り当て解除されたオブジェクトにアクションを送信しようとしています」のようなエラーメッセージが表示されます。

私を助けてくれてありがとう:)

于 2012-01-25T07:30:24.453 に答える
0
  [theFishDeathView addTarget:self
                 action:@selector(sellFish)
       forControlEvents:UIControlEventTouchUpInside];

あなたは書いてUIControlEventTouchDownいませんUIControlEventTouchDown

于 2012-01-22T09:58:15.413 に答える