chongzixin chongzixin - 4 months ago 14
iOS Question

iOS seating plan layout

I am creating an iOS app with a seating plan layout.

Trying to use an object-oriented approach, I created a class for

TableLayoutObjects
as they have different properties.

And to lay these
TableLayoutObjects
out on the screen I am representing them as
UIButtons
that are created as I loop through the array of
TableLayoutObjects
.

- (void) loadTables
{
for (TableLayoutObjects *layoutObjs in arrTableLayoutObjects)
{
if ([layoutObjs.shape isEqualToString:@"r"]) {
// rectangle
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
......

if(layoutObjs.isInteractable) {
[button addTarget:self action:@selector(tableTouchedDown:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(tableTouchedUpInside:) forControlEvents:UIControlEventTouchUpInside];
}
} else {
// text only. use label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(layoutObjs.posX, layoutObjs.posY, layoutObjs.width, layoutObjs.height)];
......
}
}
}


My event handlers look like the below for now.

// reverts back to original color and perform other instructions
- (void) tableTouchedUpInside:(UIButton *) button
{
button.layer.backgroundColor = [UIColor colorWithWhite:0.2f alpha:.5f].CGColor;
}


My question is: how do I identify the
UIButtons
to their
TableLayoutObjects
?
In the event handler after I change the colour of the button, I will also want to get or set some properties of the selected
TableLayoutObjects
. How can I do that?

Answer

You can set the tag of the button to the index into the arrTableLayoutObjects array of the associated item.

Alternatively, create a custom class which takes the table as a parameter and is the target of the button. This object now has direct access to the button and the table item.

Comments