我正在尝试在 IOS 中编写一个带有块的完成处理程序,但不确定它为什么不工作。
这是我到目前为止所拥有的
typedef void(^myCompletion)(BOOL);
-(void) showAnswer {
[self showAnswer:^(BOOL finished) {
if (finished) {
LnbScene *scene = (LnbScene *)gameScene.lnbScene;
//once the tiles position have been updated then move to the next riddle
[scene nextRiddle];
}
}];
}
// This method should update the position of tiles on the scene
-(void) showAnswer:(myCompletion) compblock{
LnbScene *scene = (LnbScene *)gameScene.lnbScene;
NSArray *tilesArray = [scene.tilesBoundary children];
for (Tile *tile in tilesArray) {
if (tile.positionInAnswer != 17) {
[tile removeFromParent];
[scene.spacesBoundary addChild:tile];
CGPoint targetPoint = CGPointMake(tile.targetPoint.x, tile.targetPoint.y + 6);
tile.position = targetPoint;
}
}
compblock(YES);
}
我正在从场景中调用 showAnswer 方法,如下所示:
GameViewController *gVC = (GameViewController *)self.window.rootViewController;
[gVC showAnswer];
当我逐步执行代码时,我没有遇到任何错误,并且序列按预期进行,即。磁贴位置发生更改,然后完成处理程序触发到 nextRiddle 方法。唯一的问题是没有更新任何界面。有什么我想念的吗?
我已经通过取出完成块测试了磁贴重新定位的代码是否有效,我在界面中查看它并获得所需的结果。所以我很确定问题在于我如何实现这些块。我以前从来没有写过一个块,所以我真的一头雾水。
-(void) showAnswer {
LnbScene *scene = (LnbScene *)gameScene.lnbScene;
NSArray *tilesArray = [scene.tilesBoundary children];
for (Tile *tile in tilesArray) {
if (tile.positionInAnswer != 17) {
[tile removeFromParent];
[scene.spacesBoundary addChild:tile];
CGPoint targetPoint = CGPointMake(tile.targetPoint.x, tile.targetPoint.y + 6);
tile.position = targetPoint;
}
}
}