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。

相关推荐
CodeJourney_J12 小时前
如何通过SMB协议实现手机共享电脑文件
windows·ios·smb
ipad协议开发19 小时前
企业微信iPad协议接口深度解析:技术原理、应用场景与开发实战
ios·企业微信·ipad
qAICHjag1 天前
MATLAB Simulink主动均衡电路模型下的汽车级锂电池动力模组管理 基于模糊控制策略...
swiftui
千里马-horse1 天前
Building a Simple Engine -- Mobile Development -- Platform considerations
android·ios·rendering·vulkan
systeminof2 天前
全新MacBook发布:苹果公司将 iPhone 芯片带入笔记本时代
ios·iphone
pop_xiaoli2 天前
effective-Objective-C 第四章阅读笔记
笔记·ios·objective-c·cocoa·xcode
前端不太难2 天前
Flutter 适合什么团队?RN / iOS 各自的边界在哪?
flutter·ios
带娃的IT创业者3 天前
解密OpenClaw系列10-OpenClaw系统要求
人工智能·macos·ios·objective-c·ai智能体·智能体开发·openclaw
systeminof3 天前
iOS 26.4 更新前瞻:Siri 架构级重构与上线时间预测
ios·重构