0

I'm trying to select a number of interface elements on the watch randomly (e.g. from 20 UIButtons) in order to modify their properties color etc.

On the iPhone I would have had init a bunch of UIViews with numerical tags so I loop through them or use arcforrandom to select random squares. I understand that there is no tagging in watchkit interface elements. Is there another way to achieve the above?

Jack
  • 16,677
  • 8
  • 47
  • 51
jane
  • 55
  • 4

1 Answers1

1

Since you don't have tags to work with in WKInterfaceObjects and since you can't subclass the buttons to add properties or tags, the next best thing to do is do what the Apple documentation suggests:

Do not subclass or create instances of this class yourself. Instead, define outlets in your interface controller class and connect them to the corresponding objects in your storyboard file. For example, to refer to a button object in your interface, define a property with the following syntax in your interface controller class:

OBJECTIVE-C

@property (weak, nonatomic) IBOutlet WKInterfaceButton* myButton;

This means, unhappily, that you'll need about 20 IBOutlets. You can then put these outlet pointers into an array that you can select from, randomly.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Hey Michael, thanks for answering. I tried the above already. When I tried to run, Xcode started to index, hung and was never able to build. – jane Aug 09 '15 at 13:01
  • Indexing in Xcode (which usually happens when you launch Xcode or open a project) has nothing to do with building. Does that crash happen every time you build? – Michael Dautermann Aug 09 '15 at 19:25
  • Yeah it's strange. If I comment out the IBOutlet array and clean then it indexes and builds. But otherwise it's just spin wheel and indexing completely unable to build. – jane Aug 10 '15 at 10:49
  • [Here is a related question](http://stackoverflow.com/questions/13831559/xcode-stuck-on-indexing) about the stuck on indexing problem. A solution in there might help you out! – Michael Dautermann Aug 10 '15 at 11:15
  • Maybe `IBOutletCollection` can avoid the need to create 20 `IBOutlet`. – Cœur Mar 18 '16 at 03:30