我有一个GameViewController班级和一个GameBoardView班级。
在GameViewController我从 class 中创建n X m 个对象CSquare。这将符合游戏板。这些对象被绘制在屏幕上(并实际填充它),因为对于每个正方形,我都会这样做[self.view addSubview: square]。
GameViewController是GameBoardView的代表。在里面GameBoardView我已经实现了该touchesEnded方法,这里有一条消息发送给委托(即GameViewController),以便将“控制”转移回GameViewController. 所以我从那里捕获“触摸”GameBoardView并将它们发送到GameViewController以便处理那里的任何方法。
当我从情节提要创建一个UIButton名为settingsButton. 我希望settingsButton它位于 GameBoard 的顶部,以便始终可以按下它。
但是,即使我在 中创建了一个出口IBOutlet UIButton *settingsButton和动作(IBAction)settingsButtonPressed:(id)sender,GameViewController每当我触摸屏幕时,触摸都会被 捕获touchesEnded,并且它永远不会检测到“设置按钮”被按下。实际上,该操作settingsButtonPressed从未被调用。
我尝试了几件事:
- 在创建所有方块并将其作为子视图添加到 的视图后,将其设置
settingsButton为第一响应者。没用。[_settingsButton becomeFirstResponder];GameViewController - 做
[self.view bringSubviewToFront:_settingsButton];。
它们都不起作用。知道我做错了什么吗?
更新:我设法让它工作,并意识到我的错误在哪里。
在viewDidLoad我无意中创建了两个 UIView:
GameBoardView *View = (GameBoardView*)self.view;
View.delegate = self;
(...)
GameBoardView *GBView = [[GameBoardView alloc] initwithFrame:frame];
[self.view addSubview:GBViwe];
我仍然不明白为什么[self.view bringSubviewToFron:_SettingsButton];使用上面的代码不能正常工作。但是,删除与 GBView 相关的行使一切正常。
多谢大家的评价。