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。

相关推荐
思盛iOS签名上架4 小时前
IOS企业签名作用(核心作用一)
ios·testfight
黑科技iOS上架1 天前
unity3D 应用AppStore4.3被拒怎么自查
ios·审核·unity3d混淆
用户38034165882971 天前
FFmpeg 在 iOS 上的裁剪、集成与包体控制
ios·音视频开发
黑科技iOS上架1 天前
Cocos应用App Store4.3被拒怎么自查
macos·ios·objective-c·cocoa·审核·ios混淆·cocos混淆
00后程序员张1 天前
怎么用 Egret(白鹭引擎)打包 iOS 应用并上架 App Store?
android·ios·小程序·https·uni-app·iphone·webview
风雨_1 天前
show-me + grill-me 结合使用总结
ios
东坡肘子1 天前
iPhone Duo 带来的机遇与挑战 -- 肘子的 Swift 周报 #153
人工智能·swiftui·swift
Rudon滨海渔村2 天前
iPhone上使用“库乐队”设置mp3铃声
ios·iphone·mp3·m4r·铃声
秋雨梧桐叶落莳2 天前
【iOS】GCD内容整理
macos·ios·cocoa