Why is the Button not working? SwiftUI

I want to create a button, but for some reason it does not work. The text message is not printed in the console.

import SwiftUI

struct Test: View {

    var body: some View {

        NavigationView {

            Text("My Test")

                .navigationTitle("Welcome")

                .toolbar {

                    Button("Help") {

                        print("Help tapped!")

                    }
                }
        }
    }
}

struct Test_Previews: PreviewProvider {

    static var previews: some View {

        Test()
    }
}

Accepted Reply

Works for me...
...make sure you have the Console visible, and not just the Variables View (which uses the same space, and can sometimes hide the Console)

  • Thank you for your answer!

    Where does it work for you - in the console or in the simulator? The fact is that in the simulator it also works for me. But since this is SwiftUI, I expected it to work in the console in Live Preview Canvas mode as well.

  • Ah, you did not mention Live Preview Canvas in your question. "print" statements will not log to the Console from that.

  • Yes, I have understood where is the right way! Thanks!

Add a Comment

Replies

Works for me...
...make sure you have the Console visible, and not just the Variables View (which uses the same space, and can sometimes hide the Console)

  • Thank you for your answer!

    Where does it work for you - in the console or in the simulator? The fact is that in the simulator it also works for me. But since this is SwiftUI, I expected it to work in the console in Live Preview Canvas mode as well.

  • Ah, you did not mention Live Preview Canvas in your question. "print" statements will not log to the Console from that.

  • Yes, I have understood where is the right way! Thanks!

Add a Comment

try the following, It gets rid of the layout constraint issues and shows the My Text Label, using an EmptyView as the destination and the help button in the navbar. There is a certain view hierarchy that must be kept to avoid layout issues.

struct ContentView: View {
    @State var selection: String?
    let id = "1"
    var body: some View {
        test
    }

    var test: some View {
        NavigationView {
            NavigationLink(tag: id, selection: $selection) {
                EmptyView()
                    .navigationTitle("Welcome")
            } label: {
                Text("My Test")
            }
            .toolbar {
                Button("Help") {
                    print("Help tapped!")
                }
            }
        }
    }

Example two without the NavigationLink

struct ContentView: View {
    var body: some View {
        nonavLink
    }

    var nonavLink: some View {
        NavigationView {
            Text("My Test")
            .toolbar {
                Button("Help") {
                    print("Help tapped!")
                }
            }
        }
        .navigationTitle("Welcome")
    }
}
  • Thanks for your examples

Add a Comment

Are you trying this in the simulator or in the live preview? print doesn't work in the preview.

Thank you so much guys! I appreciate your help!