0

我正在尝试创建一些垂直对齐的按钮,但我的代码似乎不起作用。当我尝试模拟器时,什么也没有出现。这是我的代码:

        NSInteger i;
for (i = 1; i <= oneValue.integerValue; i++) {
    UIButton *buttonOne = [[UIButton alloc] initWithFrame:CGRectMake(50, 450 - (30 * i), 25, 25)];
    [buttonOne setImage:[UIImage imageNamed:@"Dark Circle.png"] forState:UIControlStateNormal];
    [buttonOne setImage:[UIImage imageNamed:@"Dark Circle Highlighted.png"] forState:UIControlStateSelected];
}

NSNumber "oneValue" 是从之前的用户输入中检索出来的,它是一个从 1 到 20 的整数。

4

1 回答 1

0

您需要将按钮添加到它们的超级视图中。如果该代码在 a 中执行UIViewController,那么它应该是:

NSInteger i;
for (i = 1; i <= oneValue.integerValue; i++) {
    UIButton *buttonOne = [[UIButton alloc] initWithFrame:CGRectMake(50, 450 - (30 * i), 25, 25)];
    [buttonOne setImage:[UIImage imageNamed:@"Dark Circle.png"] forState:UIControlStateNormal];
    [buttonOne setImage:[UIImage imageNamed:@"Dark Circle Highlighted.png"] forState:UIControlStateSelected];
    [self.view addSubview:buttonOne];
}
于 2013-05-31T01:36:47.820 回答