For instance I have created a view with 50 sequential buttons named btn1-btn50. If I want to address these buttons and make some changes to them in a loop how can I address the controls with a string name?
-
1What do you mean the buttons are *named* btn1-btn50? You mean those are their title properties? Those are the names of variables you use to refer to them? Those are the nicknames you use when discussing the buttons with your buddies? Buttons don't normally have names, so your terminology is confusing. – Chuck Jun 04 '10 at 01:58
-
possible duplicate of [Objective C Equivalent of PHP's "Variable Variables"](http://stackoverflow.com/questions/2283374/objective-c-equivalent-of-phps-variable-variables) – Dave DeLong Jun 04 '10 at 02:02
-
Chuck - By name I am referring to the name in interface builder, there is clearly an attribute called name...whether it is accessible programmatically or not was basically the point of this question. I suppose it could always just be there for organization in interface builder but it seems like that should be able to be tapped into programatically. However based on the answers I have gotten I am guessing it is safe to assume that it is not. – Smallinov Jun 04 '10 at 03:18
4 Answers
The typical way to get a view in a hierarchy is to call [parentView viewWithTag:] to get the view. If you give the buttons tag values from 1 to 50 you can use that to access the buttons.
If for some reason you need strings, you will have to create a custom subclass of UIButton that has a name member, assign a name to that member, then later iterate through the view hierarchy searching for an instance of your custom class with a name matching your search criteria.

- 53,459
- 16
- 107
- 112
If you have given them a tag
, you can access them conveniently using -viewWithTag:
on the parent view.

- 97,545
- 26
- 194
- 236
You have no guarantee that these buttons are laid out in memory sequentially, so trying to use pointer arithmetic is probably out. But you can get all the subviews of an NSView with -subviews
, and just do something with the NSButtons:
for (NSView *view in [theView subviews]) {
if (![view isKindOfClass:[NSButton class]]) continue;
/// you got a button!
}

- 18,559
- 5
- 43
- 63
-
This may work because I can just loop through all of them looking for the one with the proper name, but is there no way to pass in a string to retrieve the view without looping through all of them? – Smallinov Jun 04 '10 at 01:42
In your View Contoller add an NSMutableDictionary *buttonViews property. In your viewDidLoad method, add each button to buttonViews using the name string as the key and the button as the object. You will have to use viewWithTag: already discussed to obtain the views. Now you can locate the button using the string and benefit from the collection methods and fast enumeration. Apple's documentation for Interface Builder indicates that the "name" in IB is used to assist identifying objects in IB which is helpful for translation.

- 4,170
- 1
- 21
- 22