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_915921431 小时前
没有 iOS 源码的前提下如何进行应用混淆,源码混淆失效后的替代
android·ios·小程序·https·uni-app·iphone·webview
菜的不敢吱声5 小时前
swift学习第一天
开发语言·学习·swift
2501_915106325 小时前
Perfdog 成本变高之后,Windows 上还能怎么做 iOS APP 性能测试
android·ios·小程序·https·uni-app·iphone·webview
2501_915106326 小时前
iOS 安装了证书,HTTPS 还是抓不到
android·网络协议·ios·小程序·https·uni-app·iphone
2501_924064117 小时前
2026年移动应用渗透测试流程方案及iOS与Android框架对比
android·ios
Digitally7 小时前
如何轻松地将数据从 iPhone 传输到 iPhone 17
ios·iphone
2501_916007478 小时前
iOS 自动化上架的工具组合,在多平台环境中实现稳定发布
android·运维·ios·小程序·uni-app·自动化·iphone
TO_ZRG8 小时前
iOS 符号表文件概要
macos·ios·cocoa
shankss1 天前
GetX 状态管理详解
android·flutter·ios
shankss1 天前
Flutter 项目启动全流程详解
android·flutter·ios