Search code examples
swiftmacosswiftuialertmac-catalyst

Mac Catalyst SwiftUI: Alert showing duplicate buttons


Does anyone know how to fix this bug? Otherwise I'll create my own alert modal maybe? It seems it's a old bug - at least from macOS 10.15.3, now 10.15.5 and still unresolved. What's funny is that in the first miliseconds after pushing to view it renders correctly - showing only 2 buttons, and then it breaks. Alert with multiple buttons

fileprivate func foo() -> Alert {
        return Alert(title: Text("Foo"),
                     message: Text("Are you sure you want to ..."),
                     primaryButton: .default(Text("Yes"), action: {
                        self.fooAction()
                     }), secondaryButton: .default(Text("No")))
    }

and called :

.alert(isPresented: $isShowingFoo, content:{
            foo()
        })

Solution

  • I remember having the same problem in the past, but now it shows the expected buttons. I'm using Xcode 11.5 for a mac catalyst app, targeting ios 13.5 and mac 10.15. Note this is not a macos app.

    I have the following test that shows the expected buttons. But I noticed that if I click on a button many times the app crashes after a while.

    struct ContentView: View {
    
    @State var isShowingFoo = false
    
    var body: some View {
    VStack  {
        Button(action: {
            self.isShowingFoo.toggle()
        }) {
            Text("Press to show alert")
        }
    }.alert(isPresented: $isShowingFoo) {
        foo()
    }
    }
    
    fileprivate func foo() -> Alert {
    return Alert(title: Text("Foo"),
                 message: Text("Are you sure you want to ..."),
                 primaryButton: .default(Text("Yes"), action: {
                    self.fooAction()
                 }), secondaryButton: .default(Text("No")))
    }
    
    func fooAction() {
    print("---> Yes fooAction")
    }
    }
    

    I also tried with

    secondaryButton: .default(Text("no"), action: {})