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

button not working

Forums > 100 Days of SwiftUI

Hi everyone,

I am onto day 20 project part one now and I have a querstion about buttons

I added a simple button with text and a closure.

the closure includes a simple message Text("Button is tapped"), but when I tap the button on canvas, no action after button's tapped.

I tried to put the button view under navigationView, thinking it may have to do with sliding into another page, but nothing after button's tapped.

Can someone take a look at it for me and see what I need to add? Thank you!

3      

Just tried your code and it works for me. Not sure whats going on and why you are getting the error. Just a pointer but its probably best to have your modifier for the navBarTitle outside your VStack so it encompasses your entire view. It still works the way you have it though so its up to you - I tried the following code -

struct ButtonView: View {
    var body: some View {
        NavigationView {
            VStack {
                Form {
                    navigationBarTitle(Text("Button App"))
                    Text("Content")
                        .foregroundColor(Color(red: 0.3, green: 0.8, blue: 0.2))
                        .font(.system(size: 30, design: .monospaced))

                    Button("Tap this button") {
                        print("tapped")}
                }
            }

        }
    }
}

struct ButtonView_Previews: PreviewProvider {
    static var previews: some View {
        ButtonView()
    }
}

If you are using the canvas to tap the button then you need to enable the debugger to read the print statements that are happening. If you just press the play button you wont see anything happen. To do this right click on that play button on the bottom right of your canvas and select 'Debug Preview'. It will then start and you should see the debug screen pop up from below. Once there you just tap your button and you will see your print statements.

Dave

4      

Thanks Dave. I tried using debug preview but it still didn't show anything on the buttom part of the debug area in Xcode. But its okay. At least I know the code works now.

I have another question about the above code. You see with the 2 modifiers that are used for Text (line 17). Do you know why we need to add .font // .foregroundColor in order to use the modifiers? What type do the modifiers belong to? I checked the apple swift documentations for both pages. Apple mentioned they are both instance properties but I don't understand how it needs a . before using them, also with colors sometimes, we need to use let say Color: .green with a . as well. do you happen to know why? Thanks!

3      

The .color modifier expects an instance of Color, therefore the . is required. Actually using dot syntax is just shorthand for writing Color.green in your example. It's just a quicker way to write .green instead of Color.green, since the compiler can infere the type for us.

Not sure about your first question regarding the modifiers, though, can you try to expand on that?

Nicolas

3      

Another way to look at a modifier is as a function. As with any instance of a class or a struct, if you have functions with that class or struct, you would use that function on each instance as follows -

instanceOfStructName.functionCall()

Dot syntax is used in most languages to make calls to functions. So in SwiftUI doing the following -

 Text("Content")
       .foregroundColor(Color(red: 0.3, green: 0.8, blue: 0.2))
       .font(.system(size: 30, design: .monospaced))

you are basically making 2 function calls on the instance of that Text. So you could type it like so -

 Text("Content").foregroundColor(Color(red: 0.3, green: 0.8, blue: 0.2)).font(.system(size: 30, design: .monospaced))

looks messy though which is why you find its done the way it is for readability and cleaness.

I think they are called modifiers because thats in fact what those functions do... modify the view in some way or another.

Modifiers are part of the protocol View. Quote from Apple documentation - 'A type that represents part of your app’s user interface and provides modifiers that you use to configure views.' Now all views implement the View protocol which is why they are available to all views. A modifier is not a type but just a function that a view uses to change its appearance pretty much.

I hope this answers your question.

Dave

3      

Hi

Not sure why your print("Button Tapped") does not show in console. Have you got the console open in the bottom left corner should have a both boxes in blue, but will be a setting in Xcode.

You could try changing the text in app to show it

struct ContentView: View {
    @State private var tapped = "Not tapped"

    var body: some View {
        NavigationView {
            Form {
                Text(tapped)
                    .foregroundColor(.green)
                    .font(.largeTitle)
                Button("TAP") {
                    print("Tapped")
                    self.tapped = "Tapped"
                }
            }
            .navigationBarTitle("Button App")
        }
    }
}

regarding the dot in the modifier you are calling a method that belongs the Text -

extension Text {

    /// Sets the color of this text.
    ///
    /// - Parameter color: The color to use when displaying this text.
    /// - Returns: Text that uses the color value you supply.
    public func foregroundColor(_ color: Color?) -> Text

    ETC

As @Newy11527 says they are mainly put just below the view that is modified just for ease of reading so you can see all them, as sometime it important what order the are applied.

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.