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。

相关推荐
karshey13 小时前
【IOS webview】h5页面播放视频时,IOS系统显示设置的icon
前端·ios
游戏开发爱好者814 小时前
抓包工具有哪些?代理抓包、数据流抓包、拦截转发工具
android·ios·小程序·https·uni-app·iphone·webview
bybitq15 小时前
Go函数闭包实战-复用函数
ios·golang·xcode
renke336417 小时前
Flutter 2025 跨平台工程体系:从 iOS/Android 到 Web/Desktop,构建真正“一次编写,全端运行”的产品
android·flutter·ios
Zender Han19 小时前
Flutter 中 AbsorbPointer 与 IgnorePointer 的区别与使用场景详解
android·flutter·ios
Digitally21 小时前
3种简单方法备份 iPhone 短信
ios·iphone
2501_9159214321 小时前
Flutter App 到底该怎么测试?如何在 iOS 上进行测试
android·flutter·ios·小程序·uni-app·cocoa·iphone
二流小码农21 小时前
鸿蒙开发:一个底部的曲线导航
android·ios·harmonyos