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

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      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.