值为 int i 的变量名

发布于 2024-12-21 12:19:03 字数 305 浏览 3 评论 0原文

我尝试做了这样的事情:

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

i try made something like this:

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

where UIButton *btn_i is btn_ + value of i.

it is possible in Obj-C?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

‖放下 2024-12-28 12:19:03

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

Do you mean you want to wind up with variables with names like btn_0, btn_1, etc?

No, you cannot do this. You might be better off with something like this:

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文