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。

相关推荐
SY.ZHOU9 小时前
大型工程跨全平台实践总结
flutter·ios·安卓·鸿蒙
denggun1234512 小时前
Sendable 协议-Swift 结构化并发的核心安全保障
ios·swift
奶糖的次元空间13 小时前
iOS 学习笔记 - 创建第一个APP
ios
for_ever_love__13 小时前
Objective-C 学习 NSString 和 NSMutableString的基本功能详解
学习·ios·objective-c
@大迁世界15 小时前
注意,苹果刚刚做出改变: iOS 26.4 系统强制所有设备开启“被盗设备保护”功能。
macos·ios·objective-c·cocoa
卢锡荣1 天前
LDR6021Q 车规级 Type‑C PD 控制芯片:一芯赋能,边充边传,稳驭全场景
c语言·开发语言·ios·计算机外设·电脑
2501_907136821 天前
iOS任意版本号APP下载
ios·软件需求
denggun123451 天前
结构化并发(Structured Concurrency)
开发语言·ios·swift
weixin_403810131 天前
EasyClick iOS USB版本蓝牙点击坐标代码
ios·自动化·代理模式
良逍Ai出海2 天前
Xcode 26来AI了:iOS独立开发者3步接入实测
人工智能·ios·xcode