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。

相关推荐
CocoaKier2 小时前
苹果谷歌纷纷调低官方抽成,苹果谷歌全球抽成比例汇总
ios·google·apple
前端Hardy3 小时前
用 uni-app x 重构我们的 App:一套代码跑通 iOS、Android、鸿蒙!人力成本直降 60%
前端·ios·uni-app
ITKEY_4 小时前
flutter 在iPad mini7上真机运行实战(踩坑)
flutter·ios·ipad
泉木8 小时前
Swift 从入门到精通-第三篇
swift
毛骗导演11 小时前
万字解析 OpenClaw 源码架构-跨平台应用之 iOS 应用
前端·ios·架构
songgeb12 小时前
Buildable Folder & Group & Folder Reference in Xcode
xcode·swift
鹏多多.21 小时前
Flutter使用screenshot进行截屏和截长图以及分享保存的全流程指南
android·前端·flutter·ios·前端框架
少云清21 小时前
【UI自动化测试】1_IOS自动化测试 _环境搭建
ui·ios
yijianxiangde1001 天前
iOS 无根越狱前期准备
ios
库奇噜啦呼1 天前
【iOS】Effective Objective-C第一章
开发语言·ios·objective-c