在演示文稿正在进行时尝试呈现UIViewController! - 警告 [英] Attempt to present UIViewController while a presentation is in progress!-Warning

查看:228
本文介绍了在演示文稿正在进行时尝试呈现UIViewController! - 警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个新的iOS项目,只需使用导航控制器(正确连接为入口点)和覆盖的viewDidAppear(),其中包含以下三行代码:

Assume a new iOS project, just with a navigation controller (correctly wired as entry point) and an overridden viewDidAppear() containing the following three lines of code:

            self.presentViewController(UIViewController(), animated: true, completion: nil)
            self.dismissViewControllerAnimated(true, completion: {})
            self.presentViewController(UIViewController(), animated: true, completion: nil)

执行时,该代码将发出警告Attempt在演示文稿正在进行时呈现UIViewController!当试图呈现第二个控制器时。

When executed, that code will raise a warning "Attempt to present UIViewController while a presentation is in progress!" when attempting to present the second controller.

问题:为了在调用另一个控制器之前正确解除控制器,我到底错过了什么?

Question: What exactly am I missing in order to dismiss the controller correctly before calling another controller?

推荐答案

假设您想要显示主控制器,显示控制器,关闭控制器,再次出现并关闭然后您需要链接操作以便它们按顺序发生。

Assuming you want the main controller to appear, present a controller, dismiss the controller, present again and dismiss then you need to chain the actions so they happen in order.

为防止它永远旋转,您还需要在主控制器第一次出现时才运行代码。

To prevent it spinning forever, you also need to only run the code when the main controller appears for the first time.

我不是快速的编码器,但是类似下面的东西应该有效。有一个检查以确保控制器正在被呈现或被按下,然后它使用每个操作完成来运行序列以开始下一个。这个警卫应该确保在每次解雇时,当 viewDidAppear 被调用时,它在这些情况下不会做任何事情。

I'm no swift coder, but something like the following should work. There is a check to make sure the controller is being presented or being pushed on and then it runs the sequence using each operations completion to start the next. This guard should make sure that following each dismiss when viewDidAppear gets called that it does not do anything on those occasions.

var firstTime = true;

func presentThenDismiss(finalCompletion: (() -> Void)?)
{
    presentViewController(UIViewController(), animated: true, completion : { [weak self] Void in
        // On completion of the present, we dismiss it
        dispatch_async(dispatch_get_main_queue(), {
            self?.dismissViewControllerAnimated(true, completion: { Void in
                // On completion of the dismiss, we present another
                finalCompletion!()
            })
        })
    })
}

override func viewDidLoad() {
    super.viewDidLoad()


    // We only run the modal presentation code when being presented or
    // being pushed on, NOT when exposed by a model dismiss or pop
    //
    if (firstTime){
        firstTime = false;

        self.presentThenDismiss { () -> Void in
            self.presentThenDismiss { () -> Void in
            }
        }
    }
}

这篇关于在演示文稿正在进行时尝试呈现UIViewController! - 警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆