如何在不使用界面构建器的情况下添加按钮单击视图

时间:2011-11-08 09:07:48

标签: iphone ios

我就是这样做的

 -(IBAction)clickButtonPressed:(id)sender
 {
NSLog(@"Clicked.........");
UIView *showView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:showView];
 }

此行动称为&没有任何错误,但此视图也未显示。

1 个答案:

答案 0 :(得分:0)

尝试设置视图的背景颜色以确保在没有您注意的情况下实际添加它,并在添加视图后释放视图(如果您不使用ARC)

-(IBAction)clickButtonPressed:(id)sender {
    NSLog(@"Clicked.........");
    UIView *showView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    showView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:showView];
    [showView release]; // if not ARC
}