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。

相关推荐
Digitally15 小时前
如何将文件从 iPhone 传输到 Android
android·ios·iphone
开开心心_Every15 小时前
文件数量统计工具:支持多层文件夹数量统计
游戏·微信·pdf·excel·语音识别·swift·lisp
2501_9151063216 小时前
iOS 抓包工具实战实践指南,围绕代理抓包、数据流抓包和拦截器等常见工具
android·ios·小程序·https·uni-app·iphone·webview
TheNextByte116 小时前
如何从备份还原 iPhone?
ios·iphone
恋猫de小郭16 小时前
Flutter 又迎大坑修改?iOS 26 键盘变化可能带来大量底层改动
android·flutter·ios·kotlin
摘星编程17 小时前
React Native for OpenHarmony 实战:DatePickerIOS iOS日期选择器
react native·react.js·ios
2501_9151063217 小时前
如何在 iOS 设备上理解和分析 CPU 使用率(windows环境)
android·ios·小程序·https·uni-app·iphone·webview
怀君17 小时前
Uniapp——苹果IOS离线打自定义基座教程
ios·uni-app
TheNextByte117 小时前
如何将视频从 iPhone 传输到戴尔笔记本电脑?
ios·电脑·iphone
前端不太难18 小时前
Flutter / RN / iOS,在状态重构容忍度上的本质差异
flutter·ios·重构