我正在創建帶有座位佈局圖的iOS應用程序。iOS座位圖佈局
試圖使用面向對象的方法,我爲TableLayoutObjects
創建了一個類,因爲它們具有不同的屬性。
爲了將這些TableLayoutObjects
放在屏幕上,我將它們表示爲UIButtons
,它們是在我循環訪問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)];
......
}
}
}
我的事件處理程序現在看起來像下面這樣。
// reverts back to original color and perform other instructions
- (void) tableTouchedUpInside:(UIButton *) button
{
button.layer.backgroundColor = [UIColor colorWithWhite:0.2f alpha:.5f].CGColor;
}
我的問題是:我怎麼識別UIButtons
他們TableLayoutObjects
?在更改按鈕顏色後的事件處理程序中,我還想獲取或設置所選TableLayoutObjects
的某些屬性。我怎樣才能做到這一點?
意思我在'for循環裏面設置了'button.tag = ??'?如何在'for(... in ...)'循環中獲取當前索引?或者我應該將它更改爲'for(;;)'類型的循環? – chongzixin
您可以更改循環,或使用計數器 – Wain
謝謝〜我似乎更好地理解了以前的建議,並且想要嘗試一下。我發現'tag'被描述爲一個'你可以用來識別你的應用程序中的視圖對象的整數。'這會給與應用程序中的其他標記產生衝突嗎? – chongzixin