变量名,值为int i

时间:2011-12-13 18:06:47

标签: objective-c for-loop naming-conventions value-of

我尝试做这样的事情:

for (int i=0; i<8; i++) {
    UIButton *btn_i = [[UIButton alloc] initWithFrame:CGRectMake(0 * numberRowsOfMenu, 0 * heightOfRow, btnWidth, btnHeight)];
}

其中UIButton *btn_ibtn_ + i的值。

有可能在Obj-C?

1 个答案:

答案 0 :(得分:4)

你的意思是你想结束像btn_0,btn_1等名字的变量吗?

不,你不能这样做。你可能会更喜欢这样的事情:

NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:8];
for(int i=0; i<8; i++) {
    UIButton *b  = [[UIButton alloc] initWithFrame:...];
    [butttons addObject:b];
    [b release];
}

// Now you can access the buttons array with indices, e.g:
UIButton *b = [buttons objectAtIndex:4];