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。

相关推荐
TheNextByte14 小时前
如何安全有效地清除iPad数据以便出售?
安全·ios·ipad
十二测试录6 小时前
Android和iOS测试区别
android·经验分享·ios·职场发展·ab测试
张飞签名上架8 小时前
深耕全球市场:App上架iOS与Google Play全流程指南
macos·ios·cocoa·ios上架·上架·谷歌上架
Digitally10 小时前
iPhone 无法向安卓设备发送图片:轻松解决
android·ios·iphone
阿里云云原生10 小时前
RUM 助力 iOS 应用稳定性:从异常捕获到堆栈还原的全流程分析
人工智能·阿里云·ios·云原生·rum
初级代码游戏20 小时前
iOS只剩美工了吗?时间都被遮盖看不清了
ios·界面设计·美工
2501_915918411 天前
iOS 开发中证书创建与管理中的常见问题
android·ios·小程序·https·uni-app·iphone·webview
00后程序员张1 天前
IOScer 开发环境证书包括哪些,证书、描述文件与 App ID 的协同管理实践
android·ios·小程序·https·uni-app·iphone·webview
江东小bug王1 天前
深入理解 UITabBarController:代理方法与 ViewController 生命周期的执行顺序(含 UINavigationController 场景)
ios