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_9159184122 分钟前
深入对比iOS开发中常用性能监控工具的底层原理与优缺点分析
android·ios·小程序·https·uni-app·iphone·webview
语歌5 小时前
AI 语言学习系统的工程边界:为什么 LLM 不该负责复习排期
人工智能·swift
白玉cfc14 小时前
【iOS】weak底层原理
macos·ios·cocoa
Lvan的前端笔记16 小时前
开通IOS开发账号
ios
2501_916008891 天前
iOS应用开发工具全面解析:如何选择与优化开发效率
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
a4493153621 天前
iPhone Face ID 故障的分层诊断——原深感系统与主板加密配对机制
macos·ios·电脑·cocoa·iphone
白玉cfc1 天前
【iOS】内存五大分区
macos·ios·cocoa
二流小码农1 天前
鸿蒙开发:以登录案例了解代码架构MVVM
android·ios·harmonyos
大龄秃头程序员1 天前
Swift 属性包装器进阶:从“语法糖”到“并发安全”的 5 个深坑
swift