我有一个充满单词的数据库,每个单词都包含信息。我的工作是使用按钮在屏幕上显示文字,而不使用界面生成器。我想我可以用 for 循环来做到这一点,如下所示:
for (int i=0; i <=20; i++) {
UIButton *word= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[word setTitle:@"Test" forState:UIControlStateNormal];
[word setFrame:CGRectMake(0, 0, 100, 40)];
[self.view addSubview:word];
}
这一切都是在 viewDidLoad 部分完成的。但是当我运行程序时,只显示一个按钮,那么如何让它显示所有20个按钮呢? 感谢正手, 尼古拉斯
最佳答案
它可能有用。
for (int i=0; i <=20; i++)
{
UIButton *word= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[word setTitle:@"Test" forState:UIControlStateNormal];
[word setFrame:CGRectMake(0, (i+1)*100, 100, 40)];
[self.view addSubview:word];
}
关于objective-c - 使用 Objective-C 的 for 循环在屏幕上显示按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7120008/