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。

相关推荐
大熊猫侯佩2 小时前
2026 码农漫游:AI 辅助 Swift 代码修复指南
swift·编程语言·apple
大熊猫侯佩2 小时前
Swift 6.2 列传(第十五篇):王语嫣的《万剑归宗》与 InlineArray
swift·编程语言·apple
__WanG18 小时前
如何申请 Family Controls 权限
ios·swiftui·权限·应用时间管理
杭城小刘2 天前
Weex 底层原理与 APM
ios·前端框架·weex
2501_924064112 天前
2025年移动应用渗透测试流程方案及iOS安卓测试方法对比
android·ios
鼹鼠SDN3 天前
【保姆教程】iPhone、iPad上玩电脑游戏 异地串流
ios·iphone·ipad·moonlight·sunshine·串流·科技数码
初级代码游戏3 天前
iOS开发 SwiftUI 2 : Image
ios·swiftui·swift
TheNextByte13 天前
如何在不使用 iCloud 的情况下备份 iPhone 短信
ios·iphone·icloud
denggun123453 天前
内存优化-(二)-oc&swift
ios·性能优化·cocoa·内存·swift
我要用代码向我喜欢的女孩表白3 天前
对象存储路径文件1TB以上文件比对,go语言
ios·golang·xcode