NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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)
            }
        }
    }
}

   

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)
            }
        }
    }
}

   

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.

   

Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

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.