【问题标题】:variable name with value of int i值为 int i 的变量名
【发布时间】:2011-12-13 18:06:47
【问题描述】:

我试着做了这样的事情:

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

【问题讨论】:

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


【解决方案1】:

你的意思是你想以 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];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 2021-09-26
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多