TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Need help with project 4 paper rock scissors

Forums > 100 Days of SwiftUI

I am trying to create buttons to represent paper rock scissors using emojis. I use ForEach to create each button but for in action I print the button pressed and it prints all 3. All 3 buttons are pressed instead of just the one emoji that was clicked on. I tried to not use ForEach and it does the same thing. How do I only get one button to press?

import SwiftUI

struct ContentView: View
{

    let possibleMoves = ["Paper", "Rock", "Scissors"]
    @State private var currentChoice = "Paper"
    @State private var winOrLose = false

    var body: some View
    {
        NavigationView
        {
            Form
            {
                Section
                {
                    VStack
                    {
                        HStack
                        {
                            Spacer()
                            ForEach(0..<3)
                            {
                                number in
                                Button
                                {
                                    currentChoice = possibleMoves[number]
                                    print(currentChoice)
                                } label:
                                {
                                    Text(moveImageType(move: number))
                                        .font(.system(size: 50))
                                }
                                Spacer()
                            }
                            Spacer()
                        }
                    }
                } .navigationBarTitle("Paper, Rock, Scissors")
            }

        }
    }

    func moveImageType(move: Int) -> String
    {
        if move == 0
        {
            return "🗒️"
        }
        else if move == 1
        {
            return "🪨"
        }
        else if move == 2
        {
            return "✂️"
        }
        else
        {
            return "⛔️"
        }
    }
}

func test(number: Int)
{
    print(number)
}

struct ContentView_Previews: PreviewProvider
{
    static var previews: some View
    {
        ContentView()
    }
}

2      

I tried creating separate buttons but when I print to console when a button is pressed all 3 of them are pressed at the same time every time. What am I doing wrong?

HStack
                            {
                                Spacer()
                                Button("🗒️")
                                {
                                    currentChoice = "🗒️"
                                    print(currentChoice)
                                }
                                Spacer()
                                Button("🪨")
                                {
                                    currentChoice = "🪨"
                                    print(currentChoice)
                                }
                                Spacer()
                                Button("✂️")
                                {
                                    currentChoice = "✂️"
                                    print(currentChoice)
                                }
                                Spacer()
                            } .font(.system(size: 50))
                        }

2      

So I found this thread here > https://www.hackingwithswift.com/forums/swiftui/tap-button-in-hstack-activates-all-button-actions-ios-14-swiftui-2/2952

And the answer is .buttonStyle(BorderlessButtonStyle()), now just one button is pressed.

2      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

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.