简体   繁体   English

在演示过程中尝试呈现 UIViewController!-警告

[英]Attempt to present UIViewController while a presentation is in progress!-Warning

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:假设一个新的 iOS 项目,只有一个导航控制器(正确连接为入口点)和一个包含以下三行代码的重写 viewDidAppear():

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

When executed, that code will raise a warning "Attempt to present UIViewController while a presentation is in progress!"执行时,该代码将引发警告“在演示过程中尝试呈现 UIViewController!” when attempting to present the second controller.尝试展示第二个控制器时。

Question: What exactly am I missing in order to dismiss the controller correctly before calling another controller?问题:为了在调用另一个控制器之前正确关闭控制器,我到底缺少什么?

You'll need to add some sort of delay on that initial presentViewController call as illustrated below:您需要在初始 presentViewController 调用上添加某种延迟,如下所示:

override func viewDidAppear(animated: Bool) {
    presentViewController(UIViewController(), animated: true) { () -> Void in
        self.delay(0.1, closure: { () -> () in
            self.dismissViewControllerAnimated(true, completion: nil)
        })
    }
}


func delay(delay:Double, closure:()->()) {
    dispatch_after(
        dispatch_time(
            DISPATCH_TIME_NOW,
            Int64(delay * Double(NSEC_PER_SEC))
        ),
        dispatch_get_main_queue(), closure)
}

It seems the completion block is called before the animation is truly complete.似乎在动画真正完成之前调用了完成块。

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.为了防止它永远旋转,您还需要仅在主控制器第一次出现时运行代码。

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.这个守卫应该确保在调用viewDidAppear在每次关闭之后它不会在这些情况下做任何事情。

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
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift - 警告:在演示过程中尝试在 * 上演示 * - Swift - Warning: Attempt to present * on * while a presentation is in progress 警告:尝试呈现 - Warning: Attempt to present <UINavigationController while a presentation is in progress 警告:在演示文稿正在进行时尝试显示uiimagepickercontroller - Warning: Attempt to present uiimagepickercontroller while a presentation is in progress ios7-警告:在进行演示时尝试在UINavigationController上呈现UINavigationController - ios7 - Warning: Attempt to present UINavigationController on UINavigationController while a presentation is in progress 是否可以忽略“警告:在演示过程中尝试演示……”是否安全? - Is it safe to ignore “Warning: Attempt to present … while presentation in progress”? performSegue导致在演示过程中出现警告尝试 - performSegue causes Warning Attempt to present while a presentation is in progress iOS:警告:尝试出席 <ModalViewController> 上 <ViewController> 演示文稿正在进行中 - iOS: Warning: Attempt to present <ModalViewController> on <ViewController>while a presentation is in progress 在演示过程中尝试在UITabBarController上呈现UIImagePickerController - Attempt to present UIImagePickerController on UITabBarController while a presentation is in progress iOS:无法在AppBrowser中启动cordova-&gt;“警告:尝试演示 <inAppBrowser> 上 <MainViewController> 在演示过程中” - iOS: Can't start cordova inAppBrowser -> “Warning: Attempt to present <inAppBrowser> on <MainViewController> while a presentation is in progress” Xcode5错误:警告尝试呈现 <View> 上 <OtherView> 在进行演示时 - Xcode5 error : Warning attempt to present <View> on <OtherView> while presentation is in progress
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM