【发布时间】:2020-09-14 04:43:32
【问题描述】:
问题
我在 NavigationView 中放置了一个增加计数器的简单按钮,但应用程序似乎只更新了一次 @State 值;稍后的点击被忽略。在 NavigationLink 之间切换会导致视图更新,但是为什么在 counter 发生更改后它不会自动更新?
事实
otherView 在我将其放在导航视图之外后可以正常工作。
代码
import SwiftUI
struct ContentView: View {
@State var counter = 0
var otherView: some View {
Button(action: {
counter += 1
}, label: {
Text(String(counter))
}).frame(maxWidth: .infinity, maxHeight: .infinity).padding()
}
var body: some View {
HStack {
NavigationView {
List {
NavigationLink("otherview", destination: otherView)
}.listStyle(SidebarListStyle())
}
}.frame(minWidth: 500, minHeight: 400)
}
}
【问题讨论】: