SwiftUI:更改其标签后,ToolbarItem位置:.principal中的按钮不起作用 Xcode 12 beta 6 代码:

问题描述

Xcode 12 beta 6

工具栏中有一个按钮,其标签文本绑定到状态var buttonTitle。我想点击此按钮以触发图纸视图,选择以更改绑定变量。

返回到内容视图后,按钮的标题将更新。但是,如果再次点击该按钮,它将无法正常工作。

代码:

struct ContentView: View {
    @State var show = false
    @State var buttonTitle = "button A"

    var body: some View {
        NavigationView {
            Text("Hello World!")
                .toolbar {
                    ToolbarItem(placement: .principal) {
                        Button {
                            show.toggle()
                        } label: {
                            Text(buttonTitle)
                        }
                        .sheet(isPresented: $show) {
                            SelectTitle(buttonTitle: $buttonTitle)
                        }
                    }
                }
        }
    }
}

struct SelectTitle: View {
    @Environment(\.presentationMode) var presentationMode
    @Binding var buttonTitle: String

    var body: some View {
        Button("Button B") {
            buttonTitle = "Button B"
            presentationMode.wrappedValue.dismiss()
        }
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)