我尝试使用撤消管理器测试某些代码,但显然,当在同一个运行循环中运行时,撤消会被分组。我试过关闭groupsByEvent,但它没有按预期工作。
- (NSString *)article
{
return _article == nil ? @"no article" : _article;
}
- (void)setarticle:(NSString *)article
{
if (_undoManager)
{
[(ModelGraph *)[_undoManager prepareWithInvocationTarget:self] setarticle:self.article];
[_undoManager setActionarticle:@"undo set article"];
}
NSCharacterSet *charactersToRemove = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
article = [[article componentsSeparatedByCharactersInSet:charactersToRemove] componentsJoinedByString:@""];
_article = article;
}
在我的测试中我有:
undoManager = [[NSUndoManager alloc] init];
myObject.undoManager = undoManager;
[myObject setArticle:@"Article 1"];
[myObject setArticle:@"Article 2"];
[myObject setArticle:@"Article 3"];
[myObject setArticle:@"Article 4"];
[undoManager undo];
NSLog(@"myObject.article: %@", [myObject article]); //prints noarticle
但是当代码运行时,我总会回来:"没有文章" - 当撤消发生时,它总是运行所有撤消...因为它们被添加到同一个runloop中。
我只想测试我的撤消是否正在使用测试......和想法?
也许在每次更改后强制运行循环?更改运行循环模式?
欢迎任何帮助。
谢谢,克里斯
答案 0 :(得分:0)
[undoManager setGroupsByEvent:NO];
[undoManager beginUndoGrouping];
dispatch_async (dispatch_get_main_queue(), ^{
[myObject setArticle:@"Article 1"];
[undoManager endUndoGrouping];
[undoManager beginUndoGrouping];
dispatch_async (dispatch_get_main_queue(), ^{
[myObject setArticle:@"Article 2"];
[undoManager endUndoGrouping];
[undoManager undo];
NSLog(@"myObject.article: %@", [myObject article]);
});
});