0

我已经定义了一个 uiscrollview 并使用一个 for 循环向它添加按钮,但是每当我运行程序时,我都会出现一个空白屏幕。这是我的 viewDidLoad,这是我与此相关的所有代码。

- (void)viewDidLoad
{
    [super viewDidLoad];
    scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 1000)];
    scrollView.hidden = NO;
    names = [[NSMutableArray alloc]initWithObjects:@"Excavations",@"Concrete",@"Framing",@"Insulation",@"Drywall",@"Finish Carpentry",@"Paint",@"Electrical",@"HVAC",@"Plumbing",@"Roofing",@"Tile",@"Doors",@"Windows",@"Flooring",@"Countertops",@"Landscape",@"Hardscape", nil];
    for (int i = 0; i < 18; i++) {
        UIButton *button = [[UIButton alloc]init];
        button.frame = CGRectMake(0, 50*i, 320, 50);
        button.tag = i;
        [button addTarget:self action:@selector(NewView:) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.text = [names objectAtIndex:i];
        button.hidden= NO;
        [scrollView addSubview:button];
    }

// Do any additional setup after loading the view, typically from a nib.
}
4

2 回答 2

3

您需要手动设置按钮的类型,或者只是使用它进行初始化:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

编辑:您还忘记将滚动视图添加到主视图

[self.view addSubview:scrollView];
于 2012-08-25T21:30:49.393 回答
1

您必须将 UIScrollView 添加到您的视图中。尝试

...
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 1000)];
scrollView.hidden = NO;
[self.view addSubview:scrollView]; 
...
于 2012-08-25T21:33:55.703 回答