WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Help me figure it out with @Binding

Forums > SwiftUI

Good day.

I can't understand why the name is not transferred to the "DropDown Header"?


import SwiftUI

let actionButtonTest = ModelActionButton()
private var width: CGFloat = 200
private var height: CGFloat = 40
private var paddingV: CGFloat = 10
private var paddingH: CGFloat = 10
private var radius: CGFloat = 10.0
private var shadowRadius: CGFloat = 0
private var shadowX: CGFloat = 0
private var shadowY: CGFloat = 0
private var shadow: Color = .black
private var backColor: Color = .gray
private var forColor: Color = .white

struct DropDownAction {
    var title: String
    var action: () -> ()
}

struct DropDownButton: View {
    var actionModel: DropDownAction
    var select: (DropDownAction) -> ()

    var body: some View {
        Button(actionModel.title.uppercased(), action: {
                self.select(self.actionModel)
        })
    }
}

struct DropMenu: View {
    var menuActions: [DropDownAction]
    var select: (DropDownAction) -> ()

    var body: some View {
        VStack(spacing: 10) {
            ForEach(menuActions, id: \.title) {
                DropDownButton(actionModel: $0, select: self.select)
                    .frame(width: width, height: height, alignment: .leading)
            }
        }
        .parametrsButtom()
    }
}

struct DropDownHeader: View {
    @State var select: DropDownAction
    var action: () -> ()
    var expand: Bool

    var body: some View {
        Button(action: action) {
            HStack {
                Text(select.title.uppercased())     //  ???????    What transfer @select?
                Spacer()
                Image(systemName: "chevron.\(expand ? "up" : "down")")
                Text("\(select.title)")
            }
            .frame(width: width, height: height, alignment: .leading)
        }
        .parametrsButtom()
    }
}

struct DropDownView: View {
    var menuActions: [DropDownAction]
    @State var expand = false
    @Binding var select: DropDownAction

    var body: some View {
        VStack(alignment: .center, spacing: 0) {
            DropDownHeader(select: select, action: {
                expand.toggle()
            }, expand: expand)
            if expand {
                DropMenu(menuActions: menuActions) { action in
                    //action.action()
                    select = action
                    print(action)
                    print(select)
                    self.expand = false
                }
            }
        }
        .animation(.spring())
    }
}

struct DropDownView_Previews: PreviewProvider {
    //@State static var textTest: String = action.actionButton.first?.title ?? "Error"
    @State static var select: DropDownAction = action.actionButton.first!
    static var previews: some View {
        DropDownView(menuActions: actionButtonTest.actionButton, select: $select)
    }
}

struct ParametrsButtom: ViewModifier {
    func body(content: Content) -> some View {
        content
            .padding(.vertical, paddingV)
            .padding(.horizontal, paddingH)
            .foregroundColor(forColor)
            .background(backColor)
            .cornerRadius(radius)
            .shadow(color: shadow, radius: shadowRadius, x: shadowX, y: shadowY)
    }
}

extension View {
    func parametrsButtom() -> some View {
        self.modifier(ParametrsButtom())
    }

}

//---------

import SwiftUI

let action = ModelActionButton()

@main
struct DropButtonApp: App {
    @State var select: DropDownAction = action.actionButton.first!
    var body: some Scene {
        WindowGroup {
            DropDownView(menuActions: action.actionButton, select: $select)
        }
    }
}

//---------

import Foundation

class ModelActionButton {
    let actionButton: [DropDownAction] = [
        .init(title: "First", action: {} ),
        .init(title: "Second", action: {} ),
        .init(title: "Third", action: {} )
    ]
}

1      

figured it out! confused @State and @Bindind places

1      

Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Fernando's book will guide you in fixing bugs in three real, open-source, downloadable apps from the App Store. Learn applied programming fundamentals by refactoring real code from published apps. Hacking with Swift readers get a $10 discount!

Read the book

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.