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。

相关推荐
sakiko_16 小时前
Swift学习笔记31-网络请求
网络·笔记·学习·swift
2601_9588151620 小时前
面向视疲劳缓解的手机贴膜光学系统设计:scinique®双护协同技术的工程实现
ios·智能手机·iphone·圆偏振光护眼·磁控溅射ar抗反射·iphone护眼膜
吠品1 天前
Go赋能:HTTP大文件秒传与断点续接
ios·iphone·xcode
唐诺1 天前
【无标题】
ios·属性包装器·wrappers
测试员周周2 天前
【Appium 系列】第14节-断言与验证 — Validator 的设计
android·人工智能·python·功能测试·ios·单元测试·appium
2501_916008892 天前
Mac 上生成 AppStoreInfo.plist 文件,App Store 上架
android·macos·ios·小程序·uni-app·iphone·webview
人月神话-Lee2 天前
【图像处理】高斯模糊——最优雅的模糊算法
图像处理·人工智能·算法·ios·ai编程·swift
@大迁世界2 天前
iPhone 18e,可能不再“低一档”
ios·iphone
Daniel_Coder2 天前
iOS Widget 开发-20:从旧版 API 迁移到 iOS 17+ 现代 Widget
ios·swift·widget·widgetcenter