views:

28

answers:

2

Hi guys, 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.

A: 
    [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.

Dan Ray
A: 

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"]]; 
}
aBitObvious
Yes, Yes, and Yes Thanks a lot it works!Does the tag is needed? or we can bypass that?
ciwol
Unless you define an ivar for slot1 so that you can reference it in the entire class across method calls, you'll have to use a tag. Well technically you could search through the subviews of self.view and look for a view of type UIImageView but that's more work than just setting a tag. But best thing would be define it as an ivar and make a property like Dan Ray suggests.
aBitObvious