You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI中Sheet弹出失败报错“Attempt to present .... while a presentation is in progress”的问题求助

SwiftUI中Sheet弹出失败报错“Attempt to present .... while a presentation is in progress”的问题求助

大家好,我目前在维护一个SwiftUI项目,项目里采用了集中式的Sheet和全屏弹窗管理方案,核心代码如下:

import SwiftUI

struct CurrentTabKey: EnvironmentKey {
  static let defaultValue: Binding<MainTabView.TabItem> = .constant(.home)
}

struct PresentedSheetKey: EnvironmentKey {
  static let defaultValue: Binding<PresentedSheet?> = .constant(nil)
}

struct PresentedFullscreenKey: EnvironmentKey {
  static let defaultValue: Binding<PresentedFullscreen?> = .constant(nil)
}

extension EnvironmentValues {
  var currentTab: Binding<MainTabView.TabItem> {
    get { self[CurrentTabKey.self] }
    set { self[CurrentTabKey.self] = newValue }
  }

  var presentedSheet: Binding<PresentedSheet?> {
    get { self[PresentedSheetKey.self] }
    set { self[PresentedSheetKey.self] = newValue }
  }

  var presentedFullscreen: Binding<PresentedFullscreen?> {
    get { self[PresentedFullscreenKey.self] }
    set { self[PresentedFullscreenKey.self] = newValue }
  }
}

最近遇到了一个棘手的问题:当尝试弹出Sheet时,偶尔会触发错误提示Attempt to present .... while a presentation is in progress。尤其是在快速连续触发弹窗操作,或者当前已有一个Sheet/全屏弹窗还处于过渡动画状态(尚未完全消失)的时候,这个错误出现的概率特别高。

我尝试过在触发弹窗前先检查presentedSheet是否为nil,但还是会有偶发的情况发生——推测是因为弹窗的过渡动画还没结束,状态值还没同步更新导致的。

有没有有经验的大佬能给我一些建议?在这种集中式管理弹窗的场景下,怎么才能有效避免这种弹窗冲突的问题呢?

备注:内容来源于stack exchange,提问作者Ali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.16 11:23:06