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

Commands in SwiftUI 2.0 do not working

Forums > SwiftUI

@micho  

Hello,
I try to wor with keyboard shortcuts with commands in SwiftUI 2.0. Here is the sample code, which does not work. Any Idea, what's wrong?

@main struct app: App {
  var body: some Scene {
    WindowGroup {
      ContentView()
    }
    .commands {
      CommandMenu("CustomCommands") {
        Button("command1",action:{print("command 1 executed")})
        .keyboardShortcut("x", modifiers: .command)
        Button("command2",action:{print("command 2 executed")})
        .keyboardShortcut("A")
      }
    }
  }
}

The keyboard commands are not received on the iPad or on the Mac under Catalyst. A menu entry does not appear on the Mac (Catalyst) as expected.

Xcode: Version 12.0 beta 2 (12A6163b). MacOS: 11.0 Beta (20A4300b)

2      

I have the same problem :/

3      

Same issue here on beta 5, both in simulator and iPad Pro 11".

2      

I have a similiar problem. In Beta 4 this code worked fine. Now with Beta 5 the sheets don't show anymore. Button does work, though. The action is executed.

var body: some View {
        List(menuObjects.menuItems, children: \.menuItems) { menuRow in
            SidebarItem(menuItem: menuRow)
        }
        .navigationTitle("Base Data".localized)
        .listStyle(SidebarListStyle())
        .toolbar {
            Menu {
                Button("Patient".localized, action: {
                    self.showAddPatientView.toggle()
                }).sheet(isPresented: $showAddPatientView) {
                    AddPatientView(isPresented: $showAddPatientView).environment(\.managedObjectContext, moc)
                }
                Button("Contact".localized, action: {
                    self.showAddContactView.toggle()
                }).sheet(isPresented: $showAddContactView) {
                    AddContactView(isPresented: $showAddContactView).environment(\.managedObjectContext, moc)
                }
                Button("Medicament".localized, action: {
                    self.showAddMedicamentView.toggle()
                }).sheet(isPresented: $showAddMedicamentView) {
                    AddMedicamentView(isPresented: $showAddMedicamentView).environment(\.managedObjectContext, moc)
                }
            } label: {
                Label("+", systemImage: "plus")
            }
        }
        }

3      

Add on a WindowGroup

.commands {
    AppCommands()
}

And implement struct:

struct AppCommands: Commands {

    func next() {}
    func prev() {}

    @CommandsBuilder var body: some Commands {
        CommandMenu("Navigation") {
            Button(action: {
                next()
            }) {
                Text("Next")
            }

            Button(action: {
                prev()
            }) {
                Text("Previous")
            }
        }
    }
}

It works on Version 12.2 beta 3 (12B5035g) on macOS 11.0.1 (20B5012d)

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!

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.