停止执行,直到完成上述方法?

时间:2013-02-26 08:33:32

标签: objective-c ios5.1

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

- (IBAction) onPressed: (id) sender {

   [self openMyDelegateToSeeIfIAmReady];

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

现在,这不起作用。 AmIReady是布尔值,在YES中更改为openMyDelegateToSeeIfIAmReady。问题是,在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

1 个答案:

答案 0 :(得分:0)

如果要在显示模态视图控制器后运行某些代码,请使用-[ 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 ] ;
    }]
}