【问题标题】:How to make a method to wait till another method complete it work in Objective C?如何让一个方法等到另一个方法完成它在Objective C中工作?
【发布时间】:2013-06-06 11:23:46
【问题描述】:

考虑这种情况。

-(void) mainMethod
{
   [self subMethod1];
   [self subMethod2];
}

我的假设:

调用mainMethod时,会先调用subMethod1,再调用subMethod2。但是,没有规则说“仅在完成subMethod1 之后,应该调用subMethod2”。如果我在subMethod1 中使用一些延迟线或一些持续时间的动画,控件将不会等待它完成。它只会开始下一个过程。因此,subMethod2 可以在 subMethod1 完成其工作之前被调用。我说的对吗?

问题:

我怎样才能让 subMethod2 等待,直到 subMethod1 完成它的工作?

注意:我不想将subMethod2 放入subMethod1

【问题讨论】:

  • 查看块或 GCD
  • 只是好奇,这行得通吗? [[NSOperationQueue mainQueue] performSelector:@selector(subMethod1) onThread:[NSThread mainThread] withObject:self waitUntilDone:YES]; 并在下一行正常调用subMethod2
  • @matsr,我认为这可能有效,但也会阻止 UI,使用完成块或 GCD 你不会阻止 UI

标签: objective-c methods completion-block


【解决方案1】:

您应该使用完成块(如 UIView 动画具有在动画发生后执行的块)。

如果您不熟悉块,请查看Apple's Short Practical Guide to Blocks

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-10
  • 1970-01-01
  • 2018-09-26
相关资源
最近更新 更多