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。

相关推荐
徐赛俊11 小时前
IOS快捷指令:一键静音 / 恢复
ios
2501_9159184115 小时前
常见 iOS 抓包工具的使用,从代理抓包、设备抓包到数据流抓包
android·ios·小程序·https·uni-app·iphone·webview
handsome091616 小时前
uniapp打包的app,报This app was built with the iOS 18.1 SDK解决方案
ios·uni-app
大熊猫侯佩19 小时前
Swift 6 驱魔实录:揭开 Combine 与 @Sendable 的“血色契约”
swift·block·combine·preconcurrency·sendable·mainactor·isolation
初级代码游戏20 小时前
iOS开发 SwiftUI 15:手势 拖动 缩放 旋转
ios·swiftui·swift
ujainu1 天前
Flutter + OpenHarmony 游戏开发进阶:虚拟摄像机系统——平滑跟随与坐标偏移
开发语言·flutter·游戏·swift·openharmony
森之鸟1 天前
iOS云打包之Shorebird
ios
GuokLiu1 天前
260203-OpenWebUI-在Windows上和RHEL上部署Caddy的步骤+在iPhone上操作的步骤
windows·ios·iphone
2501_915921432 天前
傻瓜式 HTTPS 抓包,简单抓取iOS设备数据
android·网络协议·ios·小程序·https·uni-app·iphone
恋猫de小郭2 天前
Flutter 在 Android 出现随机字体裁剪?其实是图层合并时的边界计算问题
android·flutter·ios