1

In my code, my plan was to create a random number generator. It would generate a certain function depending on which button you tapped and then in that function it would update the variable and display the number that was generated. I also wanted to add left or right to the mix so i turned all of the ints into strings so that i could display both words and ints. But when i run the code all it does is constantly generate left or right.

struct ContentView: View {

@State private var Option = ["1-10","1-100","L or D","Custom"]
@State private var Random = 0
@State private var num = 0

@State private var Ten = 0
@State private var Hundred = 0

@State private var LD = ["Left","Right"]


@State private var Title = ""
@State private var Update = ""

var body: some View {
    NavigationView {
        Form {
            Section {
                Text("\(Update)")
            }
            .padding(150)

            Section {
                Spacer()

                HStack {
                    ForEach(0 ..< Option.count) { number in
                        Button(action: {
                            self.MyButton(number)
                        }) {

                            Text(self.Option[number])
                            .padding()
                                .background(Color.purple)
                            .cornerRadius(60)
                                .foregroundColor(.white)
                            .padding(10)
                                .overlay(Circle().stroke(Color.black, lineWidth: 4))


                        }
                    }
                }
            }
        }
    .navigationBarTitle("We Need Help")
    }
}
func MyButton(_ number: Int) {
    if number == 0 {
        OneToTen()
    } else if number == 1 {
        OneToHundred()
    } else if number == 2 {
        LeftOrRight()
    } else if number == 3 {
        //add custom crap here
    }
}
func OneToTen() {
    Update = ""
    Random = Int.random(in: 1...10)
    let welp = String(Random)
    Update = welp
}

func OneToHundred() {
    Update = ""
    Random = Int.random(in: 1...100)
    let welp = String(Random)
    Update = welp
}

func LeftOrRight() {
    Update = ""
    Random = Int.random(in: 0...1)
    let welp = LD[Random]
    Update = welp

}

}
  • All four of your buttons are getting triggered. Add `.buttonStyle(BorderlessButtonStyle())` to the `HStack` containing your buttons. – vacawama Jan 17 '20 at 01:35

0 Answers0