UIScrollview没有添加按钮

时间:2012-08-25 21:27:29

标签: iphone uiscrollview uibutton

我已经定义了一个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.
}

2 个答案:

答案 0 :(得分:3)

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

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

编辑:您也忘了将滚动视图添加到主视图

[self.view addSubview:scrollView];

答案 1 :(得分:1)

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

...
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 1000)];
scrollView.hidden = NO;
[self.view addSubview:scrollView]; 
...