I have a simple counter app for my personal use. It has 5 buttons; 1st adds one to the score, 2nd adds 2, 3rd adds 3, 4th resets the score, and the 5th takes 1 off the score. I have extracted all 5 to their own variables. This totals 5 variables. Is it possible to extract to one variable and configure it to satisfy all five buttons? I have attached the file for information. Any suggestions are appreciated. Thanks
import SwiftUI
struct Counter: View {
@State var score = 0
var body: some View {
NavigationView {
ZStack{
Rectangle()
.fill(Color.blue)
.ignoresSafeArea()
VStack{
Spacer()
Text(String(score))
.font(.system(size: 160, weight: .bold))
.fontWeight(.heavy)
.foregroundColor(Color.white)
.padding()
Spacer()
HStack{
addOneView
.padding()
addTwoView
.padding()
addThreeView
.padding()
}
.padding(40)
HStack {
resetView
minusOneView
}
.padding()
Spacer()
}
}
.navigationBarTitle("Simple Counter")
}
}
var addOneView: some View {
ZStack {
Button(action: {
addOne()
}) {
Text("+1")
.fontWeight(.bold)
.font(.title)
.frame(width: 100, height: 100)
.background(Color.purple)
.cornerRadius(40)
.foregroundColor(.black)
}
Circle()
.stroke(Color.black, lineWidth: 8)
.frame(width:100, height: 100)
}
}
var addTwoView: some View {
ZStack {
Button(action: {
addTwo()
}) {
Text("+2")
.fontWeight(.bold)
.font(.title)
.frame(width: 100, height: 100)
.background(Color.green)
.cornerRadius(40)
.foregroundColor(.black)
}
Circle()
.stroke(Color.black, lineWidth: 8)
.frame(width:100, height: 100)
}
}
var addThreeView: some View {
ZStack {
Button(action: {
addThree()
}) {
Text("+3")
.fontWeight(.bold)
.font(.title)
.frame(width: 100, height: 100)
.background(Color.yellow)
.cornerRadius(40)
.foregroundColor(.black)
}
Circle()
.stroke(Color.black, lineWidth: 8)
.frame(width:100, height: 100)
}
}
var resetView: some View {
ZStack {
Button(action: {
reset()
}) {
Text("Reset")
.fontWeight(.bold)
.font(.title)
.frame(width: 150, height: 80)
.background(Color.red)
.cornerRadius(40)
.foregroundColor(.black)
}
Capsule()
.stroke(Color.black, lineWidth: 8)
.frame(width:150, height: 80)
}
}
var minusOneView: some View {
ZStack {
Button(action: {
minusOne()
}) {
Text("-1")
.fontWeight(.bold)
.font(.title)
.frame(width: 150, height: 80)
.background(Color.gray)
.cornerRadius(40)
.foregroundColor(.black)
}
Capsule()
.stroke(Color.black, lineWidth: 8)
.frame(width:150, height: 80)
}
}
func addOne() {
score += 1
}
func addTwo() {
score += 2
}
func addThree() {
score += 3
}
func reset() {
score = 0
}
func minusOne() {
score -= 1
}
}
struct CounterView_Previews: PreviewProvider {
static var previews: some View {
Counter()
}
}
Yes; it's simple: Just create a function that returns some View and that accepts parameters for the features of the buttons that are different.
Thanks Peter. I will give that a go.
Something like this??
func buttonName(item1: Int, item2: Int) -> some View {
button code
}
Yes, except it creates a button, not the name of a button, so it shouldn't be called buttonName.
Thanks Peter. Sorry to be a pest. I have managed to construct the func to accept the color and text ok. I am having problems passing the button functions (there are 5 at the bottom) into my new function. Here is the part of the code that is relevant. I have called the parameter 'using', but am not sure of the construct from there. Can you help with this please?
func button(name: String, color: Color, using: addOne() -> some View) -> some View {
var buttonView: some View {
ZStack {
Button(action: {
using
}) {
Text(name)
.fontWeight(.bold)
.font(.title)
.frame(width: 100, height: 100)
.background(color)
.cornerRadius(70)
.foregroundColor(.black)
}
Circle()
.stroke(Color.black, lineWidth: 5)
.frame(width:150, height: 100)
}
// Text(name)
// .background(color)
// .font(.title)
}
return buttonView
}
func addOne() {
score += 1
}
func addTwo() {
score += 2
}
func addThree() {
score += 3
}
func reset() {
score = 0
}
func minusOne() {
score -= 1
}