0

あなたの助けが必要です。私はゲームを構築しようとしています。ヒント ボタンが押されると、次のコードを使用して新しいビューがプログラムで作成されます。

私のgameController.mで

//connect the Hint button
-(void)setHud:(HUDView *)hud
{
    _hud = hud;
    [hud.btnHelp addTarget:self action:@selector(actionHint) forControlEvents:UIControlEventTouchUpInside];
}

//the user pressed the hint button
-(void)actionHint
{
    CGRect  viewRect = CGRectMake(kScreenWidth/2 -kMenuWidth, kScreenHeight/2-kMenuHeight,kMenuWidth*10, kMenuHeight*2);
    HintMenu* hintMenu = [HintMenu viewWithRect:viewRect];
    [self.gameView addSubview:hintMenu];
}

これは、プログラムで作成した新しいビューです。

+(instancetype)viewWithRect:(CGRect)r
{
    HintMenu* hint = [[HintMenu alloc] initWithFrame:r];
    hint.userInteractionEnabled = YES;

    UIImage* image=[UIImage imageNamed:@"btn"];

    hint.btnHelp = [UIButton buttonWithType:UIButtonTypeCustom];
    [hint.btnHelp setTitle:@"button" forState:UIControlStateNormal];
    hint.btnHelp.titleLabel.font = kFontHUD;
    [hint.btnHelp setBackgroundImage:image forState:UIControlStateNormal];
    hint.btnHelp.frame = CGRectMake(50, 30, image.size.width, image.size.height);
    hint.btnHelp.alpha = 0.8;
    [hint addSubview: hint.btnHelp];

    UIImage* image2=[UIImage imageNamed:@"tile"];
    hint.imageHelp=[[UIImageView alloc] initWithFrame:CGRectMake(image.size.width + 50 + 10, 30, image2.size.width, image2.size.height/2)];
    hint.imageHelp.image=image2;
    [hint addSubview:hint.imageHelp];



    return hint;
}

新しいビューのボタンがいつ押されているかを特定したい。上記と同様に、GameController.mに追加します。

//connect the button in the Menu
-(void)setHintMenu:(HintMenu *)hintMenu
{
    _hintMenu = hintMenu;
    [hintMenu.btnHelp addTarget:self action:@selector(actionHintMenu) forControlEvents:UIControlEventTouchUpInside];
}

//the user pressed the menu button
-(void)actionHintMenu
{
    NSLog(@"Button in menu pressed");
}

しかし、ログは表示されません。これについて私を助けてもらえますか?

編集:このコードが、次のような私の新しい Created ビューである HintMenu.m に追加されている場合:

+(instancetype)viewWithRect:(CGRect)r
{
...
hint.btnHelp.alpha = 0.8;
    [hint.btnHelp addTarget:self action:@selector(actionHintMenu:) forControlEvents:UIControlEventTouchUpInside];
    [hint addSubview: hint.btnHelp];
..
}
//the user pressed the menu button
-(void)actionHintMenu:(UIButton *) button
{
    NSLog(@"Button in menu pressed");
}

このエラーが発生しています:

2013-06-16 16:44:30.559  *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[HintMenu actionHintMenu]: unrecognized selector sent to class 0xe6f8'
*** First throw call stack:
4

2 に答える 2

1

ビュー[hintMenu.btnHelp addTarget:self action:@selector(actionHintMenu) forControlEvents:UIControlEventTouchUpInside];のセッターメソッド内に追加した理由がわかりません。hintMenu

andの setter メソッドの呼び出しがないhintMenuため、ボタンのターゲット アクションは設定されません。

完全に削除することをお勧め-(void)setHintMenu:(HintMenu *)hintMenuします。アップデートactionHint

-(void)actionHint
{
   CGRect  viewRect = CGRectMake(kScreenWidth/2 -kMenuWidth, kScreenHeight/2-kMenuHeight,kMenuWidth*10, kMenuHeight*2);
   HintMenu* hintMenu = [HintMenu viewWithRect:viewRect];
   [hintMenu.btnHelp addTarget:self action:@selector(actionHintMenu) forControlEvents:UIControlEventTouchUpInside];
   [self.gameView addSubview:hintMenu];
}

これは単なる修正です。正しい方法を尋ねられた場合は、カスタム ビュー内でボタン ターゲットを設定することをお勧めします。そのために、ビューでデリゲート プロパティを維持できます。

于 2013-06-16T13:50:47.023 に答える
1

編集した質問への回答

交換

[hint.btnHelp addTarget:self action:@selector(actionHintMenu) forControlEvents:UIControlEventTouchUpInside];

[hint.btnHelp addTarget:hint action:@selector(actionHintMenu) forControlEvents:UIControlEventTouchUpInside];

于 2013-06-16T13:54:25.320 に答える