简体   繁体   中英

UIButton modify image of UIImageView

I'm developing an iPhone app, and I need a little help. I created a button and I want when I click on to change an image's imageView by the button's one.

the image and button :

CGRect slot1Rect = CGRectMake(70, 90, 80, 110);
    UIImageView *slot1 = [[UIImageView alloc] initWithFrame:slot1Rect];
    [slot1 setImage:[UIImage imageNamed:@"carte_dos_rouge.png"]];
    slot1.opaque = YES; 
    [self.view addSubview:slot1];
    [slot1 release];



    UIButton *carte1J1=[UIButton buttonWithType:UIButtonTypeCustom];
    carte1J1.frame=CGRectMake(60, 240, 50, 73.0);
    [carte1J1 setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png",[d getlist1:0]] ] forState:UIControlStateNormal] ;
    [carte1J1 addTarget:self action:@selector (clicCarte1J1)  forControlEvents:UIControlEventTouchUpOutside];
     [self.view addSubview:carte1J1];

the action:

- (void)clicCarte1J1:(UIImageView *)slot1 {

  [slot1 setImage:[UIImage imageNamed:@"K_spades.png"]]; }

My problem is when I click on carte1J1 nothing happens, I should have the K_spades.png image on the slot1 UIImageView.

    [carte1J1 addTarget:self action:@selector (clicCarte1J1)  forControlEvents:UIControlEventTouchUpOutside];

...

- (void)clicCarte1J1:(UIImageView *)slot1 {

  [slot1 setImage:[UIImage imageNamed:@"K_spades.png"]]; 
}

What makes you think that the UIImageView you want to modify will magically get passed to that selector? Cause it won't. Further, you've pointed your button at the method clicCarte1J1 and then implemented clicCarte1j1: , which is a different method name entirely.

What you need to do is make a @property for that UIImageView and get to it that way inside your target method. And change the method name to -(void)clickCarte1J1 without the argument.

There are these problems:

  • the @selector doesn't match the actual method (needs colon at end "clickCarte1J1:")
  • you probably want UIControlEventTouchUpInside not UIControlEventTouchUpOutside
  • the click method assumes parameter is UIImageView but it will actually be the UIButton that was clicked

To fix this, first set the tag property on slot1 so the button's click method can find it later:

CGRect slot1Rect = CGRectMake(70, 90, 80, 110);
UIImageView *slot1 = [[UIImageView alloc] initWithFrame:slot1Rect];
[slot1 setImage:[UIImage imageNamed:@"carte_dos_rouge.png"]];
slot1.opaque = YES; 
slot1.tag = 42; //use whatever number you like
[self.view addSubview:slot1];
[slot1 release];

Then change the addTarget to this:

[carte1J1 addTarget:self action:@selector (clicCarte1J1)  forControlEvents:UIControlEventTouchUpInside];

And change the click method to this:

- (void)clicCarte1J1 {  
    UIImageView *slot1 = (UIImageView *)[self.view viewWithTag:42];
    [slot1 setImage:[UIImage imageNamed:@"K_spades.png"]]; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM