I am a newbie to SwiftUI. I have a button with some text. I want to change the text of that button when some external event happens. For example take the string typed in a Text Box and show that as text in the button.
Please share a way to do this. Thanks
Please share a way to do this. Thanks
Here's how to make the example you proposed:
I suggest that you read about the @State property wrapper and how SwiftUI Views work in general.
In short, SwiftUI is a state-driven UI framework, meaning that every variable which is somehow displayed on screen that you change at some point in time will immediate refresh the view with the new data. It makes updating things and listening to changes an absolute breeze.
I hope I helped!
Code Block swift import SwiftUI struct ContentView: View { @State var buttonText = "" var body: some View { VStack { TextField("Text Here", text: $buttonText) .padding() .textFieldStyle(RoundedBorderTextFieldStyle()) Button(self.buttonText) { /* code to be executed when button is pressed */ } } } }
I suggest that you read about the @State property wrapper and how SwiftUI Views work in general.
In short, SwiftUI is a state-driven UI framework, meaning that every variable which is somehow displayed on screen that you change at some point in time will immediate refresh the view with the new data. It makes updating things and listening to changes an absolute breeze.
I hope I helped!