[英]SwiftUI List with NavigationLink and Buttons
I have a List of counters - every element in the List is a counter with a number and plus/minus buttons users can click to increase or decrease the counter.我有一个计数器列表 - 列表中的每个元素都是一个带有数字和加号/减号按钮的计数器,用户可以单击以增加或减少计数器。 Now I added a NavigationLink so that users can go to a detail view of every counter.
现在我添加了一个 NavigationLink 以便用户可以 go 到每个计数器的详细视图。 The problem is now wherever you click on the list the detail view gets always pushed - even if you click on one of the buttons (counter increases then the detail view gets pushed via the NavigationLink) - I only want to use the NavigationLink if the user clicks on the number or somewhere else but of course not if the users clicks on of the buttons.
现在的问题是,无论您单击列表的何处,总是会推送详细信息视图-即使您单击其中一个按钮(计数器增加,然后通过 NavigationLink 推送详细信息视图)-我只想在用户使用 NavigationLink点击数字或其他地方,但如果用户点击按钮,当然不会。 How can this be done?
如何才能做到这一点?
NavigationView {
List {
ForEach(counters, id: \.self) { counter in
NavigationLink(destination: SingleCounterView(currentCounter: counter)) {
CounterCell(counter: counter)
}
}
}
.buttonStyle(PlainButtonStyle())
.listStyle(GroupedListStyle())
}
I've made this test, to show a CounterCell with plus and minus buttons in a NavigationView.我已经进行了这个测试,以在 NavigationView 中显示带有加号和减号按钮的 CounterCell。 If you tap on the buttons the counter increments, if you tap on the chevron or outside the buttons the destination appears.
如果您点击按钮,计数器会增加,如果您点击人字形或按钮外部,则会出现目的地。
@State var counters = ["a","b","c"]
var body: some View {
NavigationView {
List {
ForEach(counters, id: \.self) { counter in
NavigationLink(destination: Text(counter)) {
CounterCell(counter: counter)
}
}
}
.buttonStyle(PlainButtonStyle())
.listStyle(GroupedListStyle())
}.navigationViewStyle(StackNavigationViewStyle())
}
struct CounterCell: View {
@State var counter: String
@State var inc = 0
var body: some View {
HStack {
Button(action: { self.inc += 1 }) {
Text("plus")
}
Button(action: { self.inc -= 1 }) {
Text("minus")
}
Text(" counter: \(counter) value: \(inc)")
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.