0

我需要你的帮助。我正在尝试构建游戏。当按下提示按钮时,将使用以下代码以编程方式创建一个新视图:

在我的游戏Controller.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");
}

但未显示日志。你能帮我解决这个问题吗?

编辑:如果将此代码添加到 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];的 setter 方法中添加了内容。hintMenu

没有调用 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 回答