views:

37

answers:

2

Hi

Im creating and adding a grid of buttons to my custom view keyboardView as follows:

int offset = 0;
for (int row = 0; row<4; row++){
    for (int col = 0; col<13;col++) {
        offset +=1;
        UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        aButton.frame = CGRectMake(5+col*65+offset,5+row*65, 60, 60);
        [aButton setTitle:myarray[row][col] forState:UIControlStateNormal];
        [aButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];         

        [keyboardView addSubview: aButton];
    }
}

I need certain buttons to be of different sizes, like the return key or space bar. How can i get a reference to a particular button programmatically, later on in the same method? Is there an easier way than setting the tag and then calling [keyboardView viewWithTag:t]? Becauseint's are going to get confusing.

Thanks.

A: 

You can either do it with UIView tags (which don't have to get confusing, just create an enum), or if you have only a few "special" UIButtons, you can create ivars to keep references to them.

Can Berk Güder
The special buttons would be ones like tab, caps, shift, fn, ctrl, option etc... how would i use an enum in this context?
joec
enum { KeyQ, KeyW, KeyE, KeyR, KeyT, KeyY ...}; and tag them.
Can Berk Güder
how would i tag them in the for loop?
joec
A: 

You could make instance variables like UIButton *spaceBar. if you reach the Button in the two for-Iretations which is thought to be the spacebar just do spacebar = aButton. So you can later in the method just use this instance Variable which refers to the specified button. ;-) I hope it's more or less understandable. ^^

Sandro Meier
I can change the width of the buttons fine, using this method, but, and its a big but, the buttons redraw underneath my existing buttons, i need to shift everything to the right by the new width of my buttons...
joec
Than it gets complicated. I would create it in IB. Or is there any reason not to do so?
Sandro Meier
joec
Not at the same time. But you can connect one IBAction to multiple buttons.
Sandro Meier