基于NSNumber值创建多个按钮

时间:2013-05-31 01:33:20

标签: objective-c uibutton nsnumber

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

        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之间的整数。

1 个答案:

答案 0 :(得分:0)

您需要将按钮添加到超级视图中。如果该代码在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];
}