From the course: Programming for Non-programmers: iOS 17 and Swift 5

Unlock this course with a free trial

Join today to access over 24,400 courses taught by industry experts.

Setting button text

Setting button text

- [Presenter] Now that we've swapped out our text views for custom SwiftUI views, we have all 1s in our grid. So let's look at how to make each one of these buttons customizable so we'll have 1 through 9 in our grid. Go back to CalculatorButton. And we're going to create a variable right underneath where we define CalculatorButton. So after line 10, create a new line and create a new variable with the "var" keyword, and we're going to call this "buttonText" with a capital T, and then we're going to set it equal to a default value of "1'. Now, before moving on, it's really important to use that buttonText variable inside of our text view here, because if we don't, the text will still always read as a "1". So replace that "1" string with "buttonText". Remember, we don't want quotes around it, or else it will say "buttonText". And then go back to the button grid. Now for each of these buttons, we can put our cursor in the parentheses, type "buttonText", and then pass in a string value…

Contents