1

我试着做了这样的事情:

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 中可能吗?

4

1 回答 1

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];
于 2011-12-13T18:16:33.853 回答