添加子视图不起作用

时间:2011-08-20 05:47:19

标签: objective-c xcode

- (IBAction)add {

    UIImageView *must = [UIImageView alloc];
    [must setImage:[UIImage imageNamed:@"themustache.png"]];  
    must.center = CGPointMake(0, 0);

    [self.view addSubview:must];
    [must bringSubviewToFront:self.view];
}

当我按下按钮时,它应该添加一个小胡子,但它不是

6 个答案:

答案 0 :(得分:1)

- (IBAction)add {

    UIImageView *must = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];

    [must setImage:[UIImage imageNamed:@"themustache.png"] forState:UIControlStateNormal];

    [self.view addSubview:must];

    [must release];
}

答案 1 :(得分:0)

你为什么要告诉必须将其父母带到前线?试试这个:     [self.view bringSubviewToFront:must];

答案 2 :(得分:0)

确定名为themustache.png的图像存在.. 这个对我有用..

-(IBAction)add {  
UIImageView *must=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0,50, 50)];  
must.image=[UIImage imageNamed:@"themustache.png"];  
[self.view addSubview:must];  
}

答案 3 :(得分:0)

试试这段代码,它会对你有所帮助 首先,设置UIImageView的框架并将该imageview添加到视图中,它将正常工作。

-(IBAction)add  
{
    UIImageView *must = [UIImageView alloc];
    must.frame = CGRectMake(10.0, 50.0, 262.0, 34.0);
    must.backgroundColor = [UIColor clearColor];
    [must setImage:[UIImage imageNamed:@"themustache.png"]];
    [self.view bringSubviewToFront:must];

}

答案 4 :(得分:0)

确保按钮连接到Interface Builder中的正确操作。另外把它放在你的add方法NSLog(@“Pressed”);中。如果已连接,则每次按下按钮时都会看到控制台中显示按下的消息。

答案 5 :(得分:0)

试试这个,让我知道,无论它是否有效,

- (IBAction)add {
UIImage *mustImage = [UIImage imageNamed:@"themustache.png"];
UIImageView *must = [[UIImageView alloc] initWithImage:mustImage];
[self.view addSubview:must];  
[mustImage release];
[must release];
}  

或者,

- (IBAction)add {
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);
UIImageView *must = [[UIImageView alloc] initWithFrame:myImageRect];
[must setImage:[UIImage imageNamed:@"themustache.png"]];
[self.view addSubview:must];  
[must release];
}

如果您从 AppDelegate.m 调用该功能,请尝试window而不是self.view
以上两点都必须适合你。