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