-6

Is this possible?

I have variables

IBOutlet UIButton * myButton1;
IBOutlet UIButton * myButton2;
IBOutlet UIButton * myButton3 
.... 
IBOutlet UIButton * myButton(n)

if I can refer to them in the cycle like for so they change title's? or other property

Update

    for (int i=0; i<n;i++) { 
[mybutton[n] setTitle:@"123" forState:UIControlStateNormal];
 }

like this, I understand code above not worked but can in objective-c do like this??

ayoy
  • 3,835
  • 19
  • 20
  • I don't understand. Can you provide an example of what you would like to do? Can't you put those in a vector and iterate through the vector? – Andy Prowl May 11 '13 at 11:02
  • Put them in an array, add an instance variable for the current button's index and then when you want to get the next button, set the current index to the current index + 1 module n, this way it will cycle and if you are on the last button, you will get the first button. – kmikael May 11 '13 at 11:04
  • possible duplicate of [Looping over similarly named UI elements - maybe by getting a variable from its name?](http://stackoverflow.com/questions/9361856/looping-over-similarly-named-ui-elements-maybe-by-getting-a-variable-from-its) – Monolo May 11 '13 at 12:39
  • possible duplicate of [ObjC equivalent of PHP's "Variable Variables"](http://stackoverflow.com/questions/2283374/objective-c-equivalent-of-phps-variable-variables), [Create multiple variables based on an int count](http://stackoverflow.com/q/2231783), [Syntax help: variable as object name](http://stackoverflow.com/q/7940809) [Is it possible to reference a variable with a string and an int?](http://stackoverflow.com/q/6049175) – jscs May 11 '13 at 19:34

2 Answers2

3

You should use IBOutletCollection to reference a group of views defined in a xib. Check out e.g. this blog post for more info.

ayoy
  • 3,835
  • 19
  • 20
  • 1
    I find this one more demonstrative: http://useyourloaf.com/blog/2011/03/28/interface-builder-outlet-collections.html – Alexei Sholik May 11 '13 at 11:14
  • @android I find [this](http://stackoverflow.com/posts/14056058/revisions) even much more, khm... "demonstrative"... –  May 11 '13 at 11:20
0

This is not a good approach. If you have that many similar buttons you'd like to reference in your XIB, you should consider using a table view.

If you have only a bunch of buttons, give them sensible names, and the problem goes away.

If you don't really need for all of them to be IBOutlets, use an array:

UIButton *buttons[N];

N should be a constant.

Alexei Sholik
  • 7,287
  • 2
  • 31
  • 41