1

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

- (IBAction) onPressed: (id) sender {

   [self openMyDelegateToSeeIfIAmReady];

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

现在,这行不通。AmIReady是一个布尔值,它变为YESin 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.

4

1 回答 1

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 ] ;
    }]
}
于 2013-02-26T18:55:28.137 回答