UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Button on the list is not working.

Forums > SwiftUI

@onqun  

Hey, I created a custom view for a list item. at its own file the buttons work but when I use the view on my list view, it does not work. Can someone explain to me why it is not working?

import SwiftUI

struct itemBox: View {
    @ObservedObject var item = Item(itemName: "", itemCount: 0)

    var body: some View {

        VStack{
            Text("\(item.itemName ?? "Enter Name")")
                .padding([.top, .leading, .trailing])
               HStack {

                Button ( action:{
                    item.add()
                }, label: {
                    Image(systemName: "arrowtriangle.up.square.fill")
                        .frame(width: 19.0, height: 19.0)
                        .imageScale(.large)
                        .foregroundColor(.accentColor)
                })
                .padding([.leading, .bottom])

                   Text("\(item.itemCount)")

                Button ( action:{
                    item.sub()
                }, label: {
                    Image(systemName: "arrowtriangle.down.square.fill")
                        .frame(width: 19.0, height: 19.0)
                        .imageScale(.large)
                        .foregroundColor(.accentColor)
                        .background(item.itemCount == 0 ? Color.gray : Color.blue)
                }).padding([.bottom, .trailing]) .disabled(item.itemCount == 0)
            }

        }.frame(width: 100.0, height: 100.0)
            .border(/*@START_MENU_TOKEN@*/Color.yellow/*@END_MENU_TOKEN@*/, width: /*@START_MENU_TOKEN@*/6/*@END_MENU_TOKEN@*/)

    }
}

import SwiftUI

struct ContentView: View {

    @State private var showingPopup = false
    @ObservedObject var item = Item(itemName: "", itemCount: 0)

    var body: some View {
        NavigationStack{
            List(Item.sample) { item in
                itemBox(item: item)
            }            .toolbar {
                ToolbarItemGroup(placement: .navigationBarLeading) {
                    Button("Add new") {
                        self.showingPopup = true
                        print("dsds")
                    }.sheet(isPresented: $showingPopup, content: {
                        ListView()
                    })
                }

                ToolbarItemGroup(placement: .navigationBarTrailing) {
                    Button("Item List") {
                        self.showingPopup = true
                        print("dsds")
                    }.sheet(isPresented: $showingPopup, content: {
                        ListView()
                    })
                }
            }

            .navigationBarBackButtonHidden(true)
        }
    }

}

2      

You will need to add .buttonStyle(.plain) to your button in itemBox. As list has also tap recoginition it overrides any taps of the button in your view. To make it work you modify it as plain style and it detects taps on it.

3      

@onqun  

Thank you very much.

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.