我有一个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
。
答案 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 ] ;
}]
}