我需要你的帮助。我正在努力建立一个游戏。按下提示按钮时,将使用以下代码以编程方式创建新视图:
在我的 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");
}
但未显示日志。你能帮我解决这个问题吗?
编辑: 如果在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:
答案 0 :(得分:1)
我不确定您为什么在[hintMenu.btnHelp addTarget:self action:@selector(actionHintMenu) forControlEvents:UIControlEventTouchUpInside];
视图的setter方法中添加了hintMenu
。
没有调用hintMenu
的setter方法,因此按钮target-action没有设置。
我建议完全删除-(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];
}
这只是一个修复。如果你问我正确的方法,我建议设置按钮目标应该在你的自定义视图中完成。为此,您可以在视图中维护委托属性。
答案 1 :(得分:1)
回答你编辑过的问题
替换
[hint.btnHelp addTarget:self action:@selector(actionHintMenu) forControlEvents:UIControlEventTouchUpInside];
与
[hint.btnHelp addTarget:hint action:@selector(actionHintMenu) forControlEvents:UIControlEventTouchUpInside];