views:

171

answers:

2

I have a program that I want to either hide or show certain UIbuttons depending on certain variables and all the buttons are named incrementally like 'button1, button2, button3.'

So I want to iterate through the buttons but I don't know how to address the button in an assignment statement using an nsstring as a variable inside a dot notation assignment, such as:

 for (int i = 1; i < weekday; i++) {
    int buttonIncrement = 0;
    NSString *tempString = [[NSString alloc] initWithFormat:
    @"calbutton%i", buttonIncrement];

 self.tempString.hidden = YES;
}

The "tempString" part of the assignment I want to tell it to insert "calbuttonx" where x is the button number.

Can you do this somehow? if so please explain.

+3  A: 

Use an array!


If you can't use an array, you can reference to a property by string with Key-Value Coding (KVC):

UIButton* button = [self valueForKey:tempString];
button.hidden = YES;
KennyTM
I was trying to use interface builder for the buttons because that is what I know, any way to use IB with the array or do you then have to layout the interface programatically?
nickthedude
@nick: I think the array needs to be populated programmatically.
KennyTM
+2  A: 

You can also assign a tag to each button in IB and get the button associated with the tag using

- (UIView *)viewWithTag:(NSInteger)tag

as defined on class UIView, for example:

for( int k = 0; k < 5; ++k ) {

    id subview = [self.view viewWithTag: k];

    if( subview ) { 

        ...
    }
}
Dirk
awesome i think this will work, ill let you know of the progress as I'm sure you can't waitto find out, lol.Thanks again!
nickthedude
@nickthedude: You're welcome.
Dirk
yes this definitely worked, although I am ending up having to rework the code anyway into a more programatic approach but I really do appreciate the help.
nickthedude