Print something on the screen

Hello there! I would like to print the number 5 on the screen when I click the Button, but I don't know how I can do it... if I write print("5") it prints me the number 5 in the debug area of Xcode. Can someone help me to figure out this?
Here's may code

Thanks a lot

Button {

        } label: {
            ZStack {
                Circle()
                    .frame(width: 90, height: 90)
                    .foregroundColor(Color(.systemBlue))
                    .opacity(0.5)                    

                Text("5")
                    .font(.largeTitle)
                    .bold()
                    .foregroundColor(.black)
            }
            .frame(width: 90, height: 90)
        }

You need to define an area to output something, maybe a Text somewhere in the View.

I've defined the text here, how can I change the value of it?

VStack{

            Text("text")

            

            Button {

                

            } label: {

                ZStack {

                    Circle()

                        .frame(width: 90, height: 90)

                        .foregroundColor(Color(.systemBlue))

                        .opacity(0.5)

                    

                    Text("5")

                        .font(.largeTitle)

                        .bold()

                        .foregroundColor(.black)

                }

                .frame(width: 90, height: 90)

            }

        }

I'm trying to do it in Swift Playgrounds and here's my code:


import PlaygroundSupport



let columns: [GridItem] = [GridItem(.flexible()),

                           GridItem(.flexible()),

                           GridItem(.flexible())]



struct ContentView: View {

    var body: some View {

        GeometryReader { geometry in

            VStack {

                Spacer(minLength: 290)

                Text("text")

                    .font(.largeTitle)

                ZStack {

                    LazyVGrid(columns: columns) {

                        Group {

                            Button {

                                

                            } label: {

                                ZStack {

                                    Circle()

                                        .frame(width: 90, height: 90)

                                        .foregroundColor(Color(.systemBlue))

                                        .opacity(0.5)

                                    //.position(x: geometry.size.width/2, y: -100)

                                    

                                    Text("5")

                                        .font(.largeTitle)

                                        .bold()

                                        .foregroundColor(.black)

                                }

                                .frame(width: 90, height: 90)

                            }

                        }

                    }

                }

            }

            

        }

                            

    }

}

PlaygroundPage.current.setLiveView(ContentView())


I've created a text and I would like that the text will change on the press of the button. For example: there's a Text() witch now has written on it "text" and when I press the blue button (you can see it in my code), I want "text" to change to number "5"

Create a

  • struct NumberView: View
  • a state var that will hold the value
  • in Button action, call NumberView(value)

I have tried, but it gives me this error…

You have not defined the view body:

struct NumberView: View {
    
    @State var number = 5
    
    var body: some View {
        Text("\(number)")
    }
}

And call

            NumberView(number: 10)  // call with the new value

I created the structure down the "let columns" above the ContentView and I called the NumberView in the Button's action and now the error it's gone. But when I play my code and click the button it doesn't show me the text. Can't understand why...

I've created a text and I would like that the text will change on the press of the button. For example: there's a Text() witch now has written on it "text" and when I press the blue button (you can see it in my code), I want "text" to change to number "5"

Thanks for showing your code. You just need to declare an @State variable to hold the content of the Text.

(Defining a struct NumberView makes your code too complex, better remove it.)

import SwiftUI //<-
import PlaygroundSupport

let columns: [GridItem] = [GridItem(.flexible()),
                           GridItem(.flexible()),
                           GridItem(.flexible())]

struct ContentView: View {
    
    @State var outputText: String = "text" //<-
    
    var body: some View {
        GeometryReader { geometry in
            VStack {
                Spacer(minLength: 290)
                Text(outputText) //<-
                    .font(.largeTitle)
                ZStack {
                    LazyVGrid(columns: columns) {
                        Group {
                            Button {
                                outputText = "\(5)" //<-
                            } label: {
                                ZStack {
                                    Circle()
                                        .frame(width: 90, height: 90)
                                        .foregroundColor(Color(.systemBlue))
                                        .opacity(0.5)
                                    //.position(x: geometry.size.width/2, y: -100)
                                    
                                    Text("5")
                                        .font(.largeTitle)
                                        .bold()
                                        .foregroundColor(.black)
                                }
                                .frame(width: 90, height: 90)
                            }
                        }
                    }
                }
            }
            
        }
        
    }
}
PlaygroundPage.current.setLiveView(ContentView())

Now it's working. Thanks to both of you!

Print something on the screen
 
 
Q