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。

相关推荐
LDyun_12 小时前
2026暑期云手机选型指南:从稳定性、成本到风控六大核心评判维度
arm开发·ios·智能手机·安卓·玩游戏
库奇噜啦呼15 小时前
【iOS】数据存储与持久化
ios
pop_xiaoli16 小时前
【iOS】离屏渲染
macos·ios·objective-c·cocoa
_瑞1 天前
深入理解 iOS 渲染原理
前端·ios
Android出海2 天前
iOS 出海新门槛:未成年人合规来了
ios·app store·ios开发·苹果开发者·ios开发者·ios出海
2501_915106322 天前
iOS 软件测试工具性能监控、日志分析 KeyMob、Instruments等
android·ios·小程序·https·uni-app·iphone·webview
木叶丸2 天前
AI 编程概念与 Harness 工程实践入门
人工智能·flutter·ios
东坡肘子2 天前
当每一次写入都有了新价格 -- 肘子的 Swift 周报 #144
人工智能·swiftui·swift
zzb15802 天前
Zed 配置 Swift / iOS 开发
开发语言·ios·swift
黑科技iOS上架3 天前
如何解决AppStore Guideline 5.6被拒
经验分享·ios·ai编程