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

SOLVED: Need some help with macOS menu items

Forums > SwiftUI

Below is a simple code to add a menu item under the "File" command. Basically, I want a subitem to be disabled when dataArray is empty and enabled when dataArray contains a value. The code runs, but does not update when I add a value to dataArray.

import SwiftUI

public var dataArray: [Int] = []

struct ContentView: View {
    @State var element: Int?
    var body: some View {
        VStack {
            TextField("Enter fisrt element", value: $element, format: .number)
                .onChange(of: element) { _ in
                    dataArray.append(element!)
                    print(dataArray)
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

and

import SwiftUI

@main
struct TestJSONApp: App {

    var body: some Scene {

        WindowGroup {
            ContentView()
        }

        .commands {
            CommandGroup(before: CommandGroupPlacement.newItem) {
                Button("Save data array to file") {
                    /* Do some stuff here */
                }
                .disabled(dataArray.isEmpty)
            }
        }
    }
}

2      

Hi, In order to update the changes you need to create a class that conforms to ObservableObject and then add a @Published var array = [Int]() inside, then create an @StateObject var in the TestJSONApp and pass that var to content view as @ObservedObject.

Class

class DataArray: ObservableObject {
    @Published var array = [Int]()
}

ContentView

struct ContentView: View {
    @ObservedObject public var dataArray: DataArray

    @State var element: Int?
    var body: some View {
        VStack {
            TextField("Enter fisrt element", value: $element, format: .number)
                .onChange(of: element) { _ in
                    dataArray.array.append(element!)
                    print(dataArray.array)
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView(dataArray: DataArray())
    }
}

MainApp

@main
struct TestJSONApp: App {
    @StateObject var dataArray = DataArray()

    var body: some Scene {
        WindowGroup {
            ContentView(dataArray: dataArray)
        }
        .commands {
            CommandGroup(before: CommandGroupPlacement.newItem) {
                Button("Save data array to file") {
                    /* Do some stuff here */
                }
                .disabled(dataArray.array.isEmpty)
            }
        }
    }
}

2      

Thank you @Hectorcrdna !

I suspected that @ObserevdObject was needed, but kept going in circles trying to make it work. Tested your code and works beautifully. Now I need to look it over and understand exactly what is going on.

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.