1

I have a view in a Watch app Im working on with 8 buttons, I would like to be able to put them into an array to make it cleaner and easier to set background images as well as set an action for each in a loop.

As far as I can tell, this isn't possible at the moment I'm trying to do something like this:

@IBOutlet var button0: WKInterfaceButton!
@IBOutlet var button1: WKInterfaceButton!
@IBOutlet var button2: WKInterfaceButton!
@IBOutlet var button3: WKInterfaceButton!
@IBOutlet var button4: WKInterfaceButton!
@IBOutlet var button5: WKInterfaceButton!
@IBOutlet var button6: WKInterfaceButton!
@IBOutlet var button7: WKInterfaceButton!


let buttons = [button0, button1, button2, button3, button4, button5, button6, button7]

for button in buttons{
  //do stuff in here
  button.setBackgroundImageNamed("image")
}
hagata
  • 11
  • 2

1 Answers1

0

You will need to create a WKInterfaceTable in your storyboard. In the storyboard create a row that has one button in the row. Now in your code you can set the number of rows to 8 and then configure the button for each row.

Stephen Johnson
  • 5,293
  • 1
  • 23
  • 37
  • Thanks, but in my case I have multiple buttons horizontally, within a group and to my knowledge, table views won't help this. – hagata Apr 18 '15 at 01:08
  • You are right for horizontal, a table won't help. You really shouldn't have more than 3 buttons across horizontally. Remember, that the simulator uses more space and makes the button feel bigger than on a real device. – Stephen Johnson Apr 20 '15 at 14:59