TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Is it a bug from SwiftUI macOS?

Forums > SwiftUI

Hello, I'm trying to add an alert on macOS: on this alert, there are TextField() and a Button that triggers a simple print. On iOS, the same View is working, but on macOS, the button is ignored: the alert is closing but the print is not displayed.

Can somebody try this, and confirm this is a bug on SwiftUI on macOS? Thanks!

2      

Do you have a code sample you can share?

I work primarily on macOS, and I haven't had problems with Button, but there are definitely some differences between SwiftUI on macOS and iOS.

2      

Hi, here is a small example. If you uncomment the TextField, nothing is printed.

import SwiftUI

struct ContentView: View {
    @State var showAlert = false
    @State var textFieldValue = ""

    func printSomething() {
        print("If I'm printed, action was working")
    }

    var body: some View {
        NavigationStack {
            Button("Open alert") {
                showAlert.toggle()
            }
        }.alert("My alert", isPresented: $showAlert) {
            // TextField("My text field", text: $textFieldValue)
            Button("OK") {
                printSomething()
            }
        }
        .navigationTitle("Test")
    }
}

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

2      

I did this and it works ok

struct ContentView: View {
    @State var showAlert = false
    @State var textFieldValue = ""

    var body: some View {
        NavigationStack {
            Button("Show Alert") {
                showAlert = true
            }
        }
        .alert("My Alert", isPresented: $showAlert) {
            TextField("Enter Here", text: $textFieldValue)

            Button {
                printSomething()
            } label: {
                Text("OK")
            }

            Button(role: .cancel) {
                // no action needed
            } label: {
                Text("Cancel")
            }

        }
    }

    func printSomething() {
        print("This what was entered in textfield: '\(textFieldValue)'")
    }
}

printed

This what was entered in textfield: 'TextFieldValue'

2      

Okay thank you!

So thanks to your example, I investigated and found that by default, on macOS, the first button have no effect. If I duplicate the button on my example, the seconds is working correctly.

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot 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.