SwiftUI:ToolbarItem 中的按钮(放置:.principal)在更改标签后不起作用
SwiftUI: button in ToolbarItem(placement: .principal) not work after change it's label
Xcode 12 测试版 6
工具栏中有一个按钮,其标签文本绑定到一个状态变量 buttonTitle
。我想点击此按钮触发 sheet 视图,select 更改绑定变量。
返回内容视图后,按钮的标题已更新。但是如果你再次点击这个按钮,它就不起作用了。
代码:
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()
}
}
}
已知 toolbar-sheet 布局问题,另请参阅 。您可以向 Apple 提交另一个反馈。
这是针对您的情况的解决方法 - 在 sheet 关闭后使用回调更新工具栏项目。使用 Xcode 12b5.
进行测试
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) {
self.buttonTitle = [=10=]
}
}
}
}
}
}
}
struct SelectTitle: View {
@Environment(\.presentationMode) var presentationMode
@State private var buttonTitle: String
let callback: (String) -> ()
init(buttonTitle: String, callback: @escaping (String) -> ()) {
_buttonTitle = State(initialValue: buttonTitle)
self.callback = callback
}
var body: some View {
Button("Button B") {
buttonTitle = "Button B"
presentationMode.wrappedValue.dismiss()
}
.onDisappear {
callback(buttonTitle)
}
}
}
将 sheet(...)
移动到 ToolbarItem 范围之外,如下所示:
NavigationView {
..
}.sheet(...)
Xcode 12 测试版 6
工具栏中有一个按钮,其标签文本绑定到一个状态变量 buttonTitle
。我想点击此按钮触发 sheet 视图,select 更改绑定变量。
返回内容视图后,按钮的标题已更新。但是如果你再次点击这个按钮,它就不起作用了。
代码:
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()
}
}
}
已知 toolbar-sheet 布局问题,另请参阅
这是针对您的情况的解决方法 - 在 sheet 关闭后使用回调更新工具栏项目。使用 Xcode 12b5.
进行测试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) {
self.buttonTitle = [=10=]
}
}
}
}
}
}
}
struct SelectTitle: View {
@Environment(\.presentationMode) var presentationMode
@State private var buttonTitle: String
let callback: (String) -> ()
init(buttonTitle: String, callback: @escaping (String) -> ()) {
_buttonTitle = State(initialValue: buttonTitle)
self.callback = callback
}
var body: some View {
Button("Button B") {
buttonTitle = "Button B"
presentationMode.wrappedValue.dismiss()
}
.onDisappear {
callback(buttonTitle)
}
}
}
将 sheet(...)
移动到 ToolbarItem 范围之外,如下所示:
NavigationView {
..
}.sheet(...)