目标c-停止执行,直到前面的方法完成



我有一个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

如果您想在模态视图控制器出现后运行一些代码,请使用-[ UIViewController presentViewController:animated:completion:]并将块传递给"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 ] ;
    }]
}

最新更新