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。

相关推荐
2501_915921435 小时前
iOS开发环境搭建详解 Xcode 配置与快蝎轻量级工具选择
ide·vscode·macos·ios·个人开发·xcode·敏捷流程
_瑞7 小时前
试图教会你用 Xcode Instruments
前端·ios·xcode
2501_9159090613 小时前
iOS应用性能监控 Instruments工具与崩溃日志分析完整指南
android·ios·小程序·https·uni-app·iphone·webview
iOS开发上架哦1 天前
Android代码混淆与iOS加固技术详解
后端·ios
2501_915918411 天前
详解iOS App上架至App Store的全流程步骤与注意事项
android·macos·ios·小程序·uni-app·cocoa·iphone
库奇噜啦呼1 天前
【iOS】weak底层实现
macos·ios·cocoa
软泡芙2 天前
【IOS】 Swift Package Manager (SPM) 指南
网络·ios·swift
黑化旺仔2 天前
iOS - 计算器仿写
ios
zhaocarbon2 天前
WKWebView 拦截 POST/PUT方法 丢失body的问题解决
ios·xcode·webview
东坡肘子2 天前
不是模型变慢了,是任务变大了 -- 肘子的 Swift 周报 #146
人工智能·swiftui·swift