【问题标题】:Stop execution until the preceding method is done?停止执行直到前面的方法完成?
【发布时间】:2013-02-26 08:33:32
【问题描述】:

我有一个 IBAction,它看起来像这样。

- (IBAction) onPressed: (id) sender {

   [self openMyDelegateToSeeIfIAmReady];

   if (AmIReady == YES)
   {
      [self doMyWork];
   }
}

目前,这不起作用。 AmIReady 是一个布尔值,它在 openMyDelegateToSeeIfIAmReady 中更改为 YES。问题是,在AmIReady 变为YES 之前,这段代码

if (AmIReady == YES)
{
   [self doMyWork];
}

被调用,doMyWork 永远不会被调用。我怎样才能让这个方法等到它完成[self openMyDelegateToSeeIfIAmReady]

编辑:

这是我在openMyDelegateToSeeIfIAmReady 中的内容

- (void) openMyDelegateToSeeIfIAmReady
{
    MyViewController *mvc = [[MyViewController alloc]initWithNibName:@"MyViewController" bundle:nil];
    mvc.delegate  = self;

    [self presentModalViewController:mvc animated:YES];
    [mvc release];
    amIReady = YES;
}

同样在这个委托(MyViewCrontroller)中,它需要用户的输入。如果输入了用户输入,我需要运行doMyWork

【问题讨论】:

  • 它已经在等待了。
  • 如果您没有在另一个线程上运行该方法,那么该函数将在到达 if 语句之前完成
  • 它应该等待;无论如何,你能发布openMyDelegateToSeeIfIAmReady的代码吗?
  • @rocky:+1 你是对的,我在为 osx 开发时遇到过很多次类似的问题,因为你没有提到你的目标(osx 或 ios)。
  • 我们需要查看openMyDelegateToSeeIfIAmReady 的来源才能回答这个问题。

标签: objective-c ios5.1


【解决方案1】:

如果您希望在呈现模态视图控制器后运行一些代码,请使用-[ UIViewController presentViewController:animated:completion:] 并将块传递给“完成”参数。

(此方法是presentModalViewController:animated:的替代方法)

将您的代码更改为:

-(IBAction)onPressed:(id)sender 
{
    MyViewController * controller = [ [ MyViewController alloc ] initWithNibName:@"MyViewController" bundle:nil ];
    controller.delegate = self;
    [ self presentViewController:controller animated:YES completion:^{
        [ self doMyWork ] ;
    }]
}

【讨论】:

  • 非常接近。在我的模态视图控制器被viewWillDisappear:ed 而不是viewWillAppear:ed 之后,我实际上正在尝试运行doMyWork
  • 那么你需要让你的模型视图控制器在你的委托被解除后向它发送一条消息......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
  • 1970-01-01
  • 1970-01-01
  • 2017-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多