SwiftUI疑难杂症(1):sheet content多次执行

在开发SwiftUIDev的时候,今天突然发现了一个弹出sheet时候content多次执行,手动dismiss也会多次执行的异常情况,能复现问题的代码如下:

Swift 复制代码
struct Test: View {
    @State private var present = false

    var body: some View {
        return VStack {
            Button("open") {
                present = true
            }
        }
        .toolbar {
            ToolbarItem {
//                Button("open") {
//                    present = true
//                }
                Menu {
                    Button("sample", action: {
                        present = true
                    })
                } label: {
                    Image(systemName: "curlybraces")
                }
            }
        }
        .sheet(isPresented: $present) {
            print("=====show sheet")
            return Text("sheet")
        }
        .navigationTitle("test")
    }
}

经测试,发现该问题仅在.toolbar { ToolbarItem { Menu {}}}.sheet结构的时候才会发生。如果把Menu换成Button,或者把.sheet移到.toolbar之前都不会发生这个问题。环境iOS16+Xcode14。

相关推荐
2501_9159090610 小时前
Charles 抓不到包怎么办?iOS 调试过程中如何判断请求路径
android·ios·小程序·https·uni-app·iphone·webview
2501_9160074710 小时前
iOS和iPadOS文件管理系统全面解析与使用指南
android·ios·小程序·https·uni-app·iphone·webview
core51212 小时前
使用 `ms-swift` 微调 Qwen3-VL-2B 详细指南
lora·微调·swift·qwen·qwen3·vl
core51213 小时前
Swift SFT Qwen-VL LoRA 微调指令详解
lora·微调·swift·qwen·vl
2501_9159214315 小时前
iOS App 开发阶段性能优化,观察 CPU、内存和日志变化
android·ios·性能优化·小程序·uni-app·iphone·webview
游戏开发爱好者816 小时前
在 iOS 开发、测试与上架过程中 如何做证书管理
android·ios·小程序·https·uni-app·iphone·webview
ii_best17 小时前
按键精灵安卓/IOS手机助手 × 手机按键 App:1 分钟搞定设备连接(超详细教程)
android·ios·智能手机·自动化·编辑器
2501_9160074717 小时前
在没有 Mac 的情况下完成 iOS 应用上架 App Store
android·macos·ios·小程序·uni-app·iphone·webview
TheNextByte117 小时前
iPhone存储空间已满?如何轻松释放iPhone空间?
android·ios·iphone
—Qeyser1 天前
Flutter 颜色完全指南
android·flutter·ios