同时取消一个视图控制器并呈现另一个视图控制器

3
当点击按钮时,我调用一个叫做MFMailComposeViewController的VC(视图控制器),当消息发送完成后,它会解除显示该视图控制器,此时我希望它呈现不同的视图控制器。但是,每次发送电子邮件后,它都会崩溃,我知道我做错了什么,但我不确定是哪里出了问题。
以下是我解除显示和呈现新视图控制器的代码。
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
[self dismissViewControllerAnimated:true completion:nil];
[self presentViewController:_emailConfirmationPage animated:YES completion:nil];}

我在想问题可能是我使用了 self,但我不确定该用什么代替它。

2个回答

5

这个崩溃是因为您要求同时开始dismiss和present动画。所以您可以通过以下两种方法来解决此问题:

  1. Wait for the first animation to complete before starting the next one. This will animate the dismissal of the current VC and animate the presentation of the new one. Of course the user would have to wait for both animations to complete before they can continue interacting with the app. To do this, present _emailConfirmationPage in the completion block of dismissing the current VC like this:

    [self dismissViewControllerAnimated:YES completion:^{    [self presentViewController:_emailConfirmationPage animated:YES completion:nil];
    }];
    
  2. Animate either dismiss OR present, but not both. This might be better because the user would have to wait only for 1 animation and that 1 animation will most probably be sufficient to ensure a fluid user experience.

    [self dismissViewControllerAnimated:NO completion:nil];
    [self presentViewController:_emailConfirmationPage animated:YES completion:nil];
    

直到现在我才意识到我的应用程序仍然崩溃。事实上,什么都没有改变。 - Riley Martindale

3
尝试将您的[self presentViewController:_emailConfirmationPage animated:YES completion:nil];}放入[self dismissViewControllerAnimated:true completion:nil];completion:块中,它会在完成关闭另一个视图控制器后执行呈现视图控制器。
就像这样:
[self dismissViewControllerAnimated:YES completion:^{
        [self presentViewController:_emailConfirmationPage animated:YES completion:nil];
    }];

您的代码无法正常工作,因为它仍在执行dismissViewController动画,而同时不能有两个动画。


我有点困惑。我应该将 [self dismissViewControllerAnimated:true completion:nil]; 放在 [self presentViewController:_emailConfirmationPage animated:YES completion:nil]; 中怎么写? - Riley Martindale
你需要将它放在block格式的nil部分中,请查看我的编辑。 - Tj3n
这不应该反过来吗?我不是想在它被解除后呈现视图控制器吗? - Riley Martindale
啊,因为我看到你先使用了 dismiss 然后才调用了 present?但这只是个想法,你可以按照自己的方式进行修改 :D - Tj3n

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接