0
- (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];
}

When I press a button it should add a mustache, but it doesn't

Alexey Gorozhanov
  • 706
  • 10
  • 20
user894725
  • 11
  • 4

6 Answers6

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];
}
0

Why are you telling must to bring its parent to the front? Try this: [self.view bringSubviewToFront:must];

user885074
  • 481
  • 2
  • 5
0

Make Sure image named themustache.png exists .. This one worked for me ..

-(IBAction)add {  
UIImageView *must=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0,50, 50)];  
must.image=[UIImage imageNamed:@"themustache.png"];  
[self.view addSubview:must];  
}
viral
  • 4,168
  • 5
  • 43
  • 68
Ravi Chokshi
  • 1,106
  • 9
  • 18
0

Try this code, it will help you.
First of all, set the frame of UIImageView and add that imageview into view, it will work fine.

-(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];

}
Nikunj Jadav
  • 3,417
  • 7
  • 38
  • 54
0

Make sure the button is connected to the right action in Interface Builder. Also put this in your add method NSLog(@"Pressed");. If it is connected you should see the Pressed message displayed in the console every time you press the button.

ms83
  • 1,804
  • 16
  • 13
  • yes the console does execute the NSLog i can see the pressed, so the button does work – user894725 Aug 20 '11 at 07:50
  • Only other thing i can suggest is to remove must.center = CGPointMake(0, 0);. That could be positioning it off the screen. – ms83 Aug 20 '11 at 08:48
  • @ms83: `must.center = CGPointMake(0, 0);` is okay. It will make the center of the image exactly at the top left corner of the screen. That's it. So, the user will be able to see only the forth (down right) part of the image. – viral Aug 20 '11 at 10:29
0

Try this and let me know, whether its working or not,

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

or this,

- (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];
}

Try, window instead of self.view if you are calling the function right from AppDelegate.m.
Both of the above, must work for you.

viral
  • 4,168
  • 5
  • 43
  • 68