Changing UIImageView Image

Discussion in 'iOS Programming' started by JRuc33, Jul 29, 2009.

  1. JRuc33 macrumors newbie

    Joined:
    Jul 29, 2009
    #1
    Code:
    - (IBAction)touch_btnAdvice:(UIButton *)sender {
            UIImage	*aceC = [UIImage imageNamed: @"black.png"];
    	[pPic10 setImage: aceC];
    }
    
    Basically, what I want to happen is when I press the button for the image to change. I have an image named "black.png" in my project, but the image won't change. Thanks in advance for the help. Don't know whats wrong with this code segment, but obviously something is wrong cuz the picture won't change.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    First, are you certain touch_btnAdvice: is getting called?
     
  3. moral-hazard macrumors regular

    Joined:
    Jul 27, 2009
    Location:
    Palo Alto, CA
    #3
    Double check Interface Builder - make sure the action for "touch up inside" on the button is linked to that function. Double check the other outlets as well, but the touch up inside action should be the first place to look.

    EDIT:
    Throw this in your function. It may seem stupid and simple but its an easy way to see if a function is being called. I spent 10 minutes today banging my head with respect to a function not doing what it was supposed to, but it turned out I had called it after a return statement and it actually never got called.
    Code:
    NSLog(@"function called");
    
     
  4. JRuc33 thread starter macrumors newbie

    Joined:
    Jul 29, 2009
    #4
    Yeah, I'm sure it gets called when I press the button because if I add a line of code like
    Code:
    [label1 setText: @"Hello World"];
    
    The label will change.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    If you're sure your action is being called, then the next thing to look at is to determine if your aceC UIImage is being properly setup. You are not getting any runtime errors or warnings around this? Are you certain that black.png is case-correct and has been copied to (not just linked to) your project? Also, how is pPic10 declared? Since we haven't seen that code, we can't be sure the issue doesn't involve that.
     
  6. icewing macrumors 6502

    Joined:
    Jun 19, 2008
    Location:
    St Louis
    #6
    I'm new to this, and I may be off base, so take this with a grain of salt....but, don't you need to specify WHERE the image is so that the program can actually find it?
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    From UIImage's imageNamed doc:
     
  8. simplymuzik3 macrumors 6502a

    Joined:
    Jan 29, 2009
    #8
    I think it's easier to do this:

    Code:
    myImage.image = [UIImage imageNamed:@"whatever.png"];
    
    Personally, that's what I would use.
     

Share This Page