views:

772

answers:

2

I'm going to have a 10x10 grid of UIButton objects. Each of these UIButtons is going to need to be referenced by the row and column number, so they should probably be stored in some type of an array.

My question: what is the easiest way to create this grid? Programmatically or through the Interface Builder? If programmatically, what would be the easiest way to access these buttons so that when they are touched, I am able to know the row and column number of the touched button?

+4  A: 

I personally don't like IB, so I recommend to do it programmatically!

Use an NSArray to store your UIButton's. Each button's index is row*COLUMNS+column.

Set the tag property to BASE+index (BASE being an arbitrary value > 0) so that you can find a button's position: index=tag-BASE; row=index/COLUMNS; column=index%COLUMNS;

- (void)loadView {
    [super loadView];

    for (NSInteger row = 0; row < ROWS; row++) {
        for (NSInteger col = 0; col < COLS; col++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [buttonArray addObject:button];
            button.tag = BASE + row * COLS + col;
            button.frame = ...;
            [button addTarget:self action:@selector(didPushButton:) forControlEvents:UIControlEventTouchDown];
            [self.view addSubview:button];
        }
    }
}

- (void)didPushButton:(id)sender {
    UIButton *button = (UIButton *)sender;
    NSInteger index = button.tag - BASE;
    NSInteger row = index / COLS;
    NSInteger col = index % COLS;
    // ...
}
squelart
This was exactly the kind of answer I was looking for. I didn't know that the "tag" property even existed... that's a huge help. Thank you very much squelart, I appreciate the thorough answer!
James Skidmore
'welcome. "tag" is quite useful, yes. The UIView message "viewWithTag:" may be used to find a sub-view by tag, e.g. button = [view viewWithTag:index] (but it would be slower than an NSArray access). By default tag==0, that's why I added the BASE number, in case there are sibling buttons or views.
squelart
Yep, tags are very handy. You can use them as direct identifiers. You can use them for dictionary lookups (after converting to NSNumber). You can use them to select a view randomly -- something like viewWithTag: (arc4random() % 8)
Amagrammer
+3  A: 

You can use a GridView from the moriarty library to help with layout - positioning each button where you want it. Building partially on squelart's sample code as a createButtonAtRow:col: method, this could work as follows:

GridView* gridview = [[GridView alloc] initWithRows:ROWS cols:COLS];
for (NSInteger row = 0; row < ROWS; ++row) {
  for (NSInteger col = 0; col < COLS; ++col) {
    [gridView addsubView:[self createButtonAtRow:row col:col]];
  }
}
[myView addSubview:gridView];
[gridView release];  // Let myView retain gridView.
Tyler