objective-c - 使用 Objective-C 的 for 循环在屏幕上显示按钮

标签 objective-c ios xcode button for-loop

我有一个充满单词的数据库,每个单词都包含信息。我的工作是使用按钮在屏幕上显示文字,而不使用界面生成器。我想我可以用 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/

相关文章:

c++ - 无法使用 Xcode 9 运行项目,许多新的 C++ 语义问题

iphone - iOS 设备上的核心日期获取请求

iOS - 在不知道其大小的情况下添加 View

ios - 谷歌地图折线无法完美呈现

ios - 强烈保留数组为空

ios - remoteControlReceivedWithEvent 不在单例类中调用

ios - 如何将数据从 Realm 传递到 ViewController

ios - RESTKit异常: No mappable object representations were found at the key paths searched

iphone - 如何通过服务器上的数组上传图片?

ios - Audiokit:如何在 Audiobus 面板覆盖层中添加开始/停止按钮及其功能?