我很难弄清楚如何将所有这些放在一起。我在Mac上有一个难题解决应用程序。您输入难题,按一个按钮,然后它尝试查找解决方案的数量,最小移动量等,因此我想保持UI更新。然后,一旦计算完成,请重新启用按钮并更改标题。
以下是按钮选择器和解决功能的一些示例代码:(请记住,我从Xcode复制/粘贴,因此可能缺少一些{}或其他拼写错误..但是它应该可以使您了解我的想法。我正在尝试做。
基本上,用户按下一个按钮,该按钮为ENABLED = NO,调用此函数来计算拼图。在计算时,请使用移动/解决方案数据更新UI标签。然后,一旦完成拼图计算,Button为ENABLED = YES;
按下按钮时调用:
- (void) solvePuzzle:(id)sender{
solveButton.enabled = NO;
solveButton.title = @"Working . . . .";
// I've tried using this as a Background thread, but I can't get the code to waitTilDone before continuing and changing the button state.
[self performSelectorInBackground:@selector(createTreeFromNode:) withObject:rootNode];
// I've tried to use GCD but similar issue and can't get UI updated.
//dispatch_queue_t queue = dispatch_queue_create("com.gamesbychris.createTree", 0);
//dispatch_sync(queue, ^{[self createTreeFromNode:rootNode];});
}
// Need to wait here until createTreeFromNode is finished.
solveButton.enabled=YES;
if (numSolutions == 0) {
solveButton.title = @"Not Solvable";
} else {
solveButton.title = @"Solve Puzzle";
}
}
沧海一幻觉
饮歌长啸