简体   繁体   English

SwiftUI 列表与 NavigationLink 和按钮

[英]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.

相关问题 如何在 SwiftUI 的列表中使用带有 swipeActions 的 NavigationLink - How to have a NavigationLink with swipeActions in a List in SwiftUI SwiftUI TapGesture 无法与 iOS 列表中的 NavigationLink 一起使用 - SwiftUI TapGesture Not Working with NavigationLink in List on iOS 无法单击列表(SwiftUI)中的NavigationLink - Can't click a NavigationLink in List (SwiftUI) 如何在 SwiftUI 列表中设置 NavigationLink - How to setup NavigationLink inside SwiftUI list SwiftUI 中不同视图的数组可在列表中作为 NavigationLink 进行迭代 - Array of different Views in SwiftUI iterable in a list as NavigationLink SwiftUI - HStack NavigationLink“按钮”分别向两个元素添加 buttonStyle? - SwiftUI - HStack NavigationLink "buttons" adding buttonStyle to both elements seperately? 列表中的 SwiftUI NavigationLink 没有使用 isActive 获得正确的详细信息页面 - SwiftUI NavigationLink in the list doesn't get the right detail page with isActive SwiftUI NavigationView在ForEach里面List里面用NavigationLink来回跳转 - SwiftUI NavigationView jumps back and forth with NavigationLink in ForEach inside List NavigationLink 不适用于 SwiftUI 列表中的 ScrollView - NavigationLink doesn't work with ScrollView inside a List in SwiftUI SwiftUI 列表在 Sheet 中的外观和行为与在 NavigationLink 中不同 - SwiftUI List looks and behaves differently in Sheet than in NavigationLink
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM