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。

相关推荐
库奇噜啦呼10 小时前
【iOS】NSOperation与NSOperationQueue
macos·ios·cocoa
二流小码农11 小时前
鸿蒙开发:实现文本渐变效果
android·ios·harmonyos
大龄秃头程序员13 小时前
Sendable 不是“能跑就行”:Swift Concurrency 下的实战踩坑与架构落地指南
swift
WiresharkClub18 小时前
抓包遇到私有协议全是乱码?抓包鹰自定义协议解码教程
ios
Lvan的前端笔记1 天前
ios:版本号和Build构建号
ios
大熊猫侯佩1 天前
WWDC26 全新 SwiftData 的 .codable 宏选项:它不是泛型专用胶水
数据库·swift·apple
大龄秃头程序员1 天前
Swift 并发实战:从 Task 到 Actor 重入性,我掉进了“逻辑不一致”的坑
swift
鹤卿1232 天前
「iOS」3GShare总结
macos·ios·objective-c·cocoa
LinXunFeng2 天前
给 Flutter 插件加上 Swift Package Manager 支持
flutter·swift