skip to Main Content

I want depending on which button is pressed to update the index number. Also when I print it, I want it to show me that it is well updated.

struct idontcare { //Named it this way cause I got mad
   @State var index: Int = 0
    
   let buttons: [String] = [ "Primary", "Blue", "Green", "Red", "Yellow", "Orange","Gray", "Purple", "Cyan", "Pink", "Teal"]
    
   let buttonColor: [Color] = [Color.primary, Color.blue, Color.green, Color.red, Color.yellow, Color.orange, Color.gray, Color.purple, Color.cyan, Color.pink, Color.teal]
    
   func showMenu() -> some View{
      return Menu("Update Color: "){
         ForEach(buttons, id: .self) { button in
            Button(action: {
               self.index = 5 //DOES NOT WORK!!
               print(self.index) //ALWAYS PRINTS 0!
            }) {
               Label(button, systemImage: "paintbrush.pointed")
               }
            }
        }
    }
}

3

Answers


  1. You code is missing the body property.
    The struct "Idontcare" has to conform to View, it might got lost while pasting to your question.

    However, if I add both to your code, everything works fine for me.

    struct Idontcare: View { //Has to conform to View
        @State var index: Int = 0
    
        let buttons: [String] = [ "Primary", "Blue", "Green", "Red", "Yellow", "Orange","Gray", "Purple", "Cyan", "Pink", "Teal"]
    
        let buttonColor: [Color] = [Color.primary, Color.blue, Color.green, Color.red, Color.yellow, Color.orange, Color.gray, Color.purple, Color.cyan, Color.pink, Color.teal]
    
            var body: some View { //was missing in your Code
                showMenu()
            }
    
    
    
    
        func showMenu() -> some View{
            return Menu("Update Color: "){
                ForEach(buttons, id: .self) { button in
                    Button(action: {
                        self.index = 5 //Works now
                        print(self.index)
                    }) {
                        Label(button, systemImage: "paintbrush.pointed")
                    }
                }
            }
        }
    }
    
    Login or Signup to reply.
  2. This should work just like what you wanted. I made a Test view, you can follow the same logic.

    import SwiftUI
    
    struct Test: View {
    
    @State var index: Int = 0
       
    let buttons: [String] = [ "Primary", "Blue", "Green", "Red", "Yellow", "Orange","Gray", "Purple", "Cyan", "Pink", "Teal"]
       
    let buttonColor: [Color] = [Color.primary, Color.blue, Color.green, Color.red, Color.yellow, Color.orange, Color.gray, Color.purple, Color.cyan, Color.pink, Color.teal]
    
    var body: some View {
        
        ForEach(0..<buttons.count, id: .self) { index in
                   Button(action: {
                      print(index)
                   }) {
                       Label(buttons[index], systemImage: "paintbrush.pointed")
                      }
        }
    }
    }
    
    Login or Signup to reply.
  3. You can try the below code. it will help you to get selected button index.

    struct idontcare : View { 
    
        @State var index: Int = 0
    
        let buttons: [String] = ["Primary", "Blue", "Green", "Red", "Yellow", "Orange","Gray", "Purple", "Cyan", "Pink", "Teal"]
    
        let buttonColor: [Color] = [Color.primary, Color.blue, Color.green, Color.red, Color.yellow, Color.orange, Color.gray, Color.purple, Color.gray, Color.pink, Color.black]
    
        var body: some View {
            showMenu()
        }
    
        func showMenu() -> some View{
                
            return Menu("Update Color: ") {
            
                ForEach(0..<buttons.count, id: .self) { index in
                
                    Button(action: {
                        self.index = index
                        debugPrint("Color Selected = (buttons[index])")
                        debugPrint("Selected Color Index = (self.index)")
                    }) {
                        Label(buttons[index], systemImage: "paintbrush.pointed")
                    }
                 }
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search