Image change on button click.

Discussion in 'iOS Programming' started by sandhya.rai, Oct 11, 2009.

  1. sandhya.rai macrumors newbie

    Joined:
    Jan 12, 2009
    #1
    Hi All,



    This way i have created my custom button and set the image.I have set the image for nonselectedImage and selectedImage above in load view.When the view is loaded the nonselected image is visible. When i press the button the event is called but it is not able to change the image set in the action method.
    starImageBtn5 = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
    starImageBtn5.frame = CGRectMake(231.0, 67.0, 20.0, 20.0);
    [starImageBtn5 setBackgroundImage:nonSelectedImage forState:UIControlStateNormal];
    [cell.contentView addSubview:starImageBtn5];
    [starImageBtn5 addTarget:self action:mad:selector(actionForStar5) forControlEvents:UIControlEventTouchUpInside];
    [starImageBtn5 release];
    This is the method where i am trying to change the image.The starOneSelected is a bool variable.
    - (void) actionForStar1
    {
    if(starOneSelected)
    {
    UIImage *image =[UIImage imageNamed:mad:"star-selected.png"] ;
    // [UIImage imageNamed:mad:"star-nonselected.png"];
    [starImageBtn5 setBackgroundImage:image forState:UIControlStateNormal];
    //starImageBtn5.currentBackgroundImage= image;
    starOneSelected = FALSE;
    }
    else
    {
    //[nonSelectedImage release];
    starImageBtn5 setBackgroundImage:selectedImage forState:UIControlStateNormal];
    //UIButton *test = starImageBtn5.
    starOneSelected = TRUE;
    }
    }
    Please look at the code and try to help me out.i have gone through all the questions posted in this forum as well as other forums but not able to know where i am going wrong.


    Thanks in advance.
    sandhya rai
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    What is the initial value for starOneSelected? Also, are you wanting the image of your button to change (toggle, really) for the non-selected state (i.e. normal)?
     
  3. sandhya.rai thread starter macrumors newbie

    Joined:
    Jan 12, 2009
    #3
    Image change on button click

    Hi ,


    Initial value of starOneselected is False. Yes I want the image to change
    from selected to non selected and vice versa.


    Thanks
    sandhya
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    You realize that the selected state is only in effect so long as you're still pressing the button, right? Once you let go, it goes back to non-selected (normal) state.
     
  5. sandhya.rai thread starter macrumors newbie

    Joined:
    Jan 12, 2009
    #5


    On Button Press there is no effect on the image
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Okay. What about if you try this?:
    Code:
    - (void) actionForStar1
    {
        if (starOneSelected)
        {
            UIImage *image = [UIImage imageNamed:@"star-nonselected.png"] ;
            [starImageBtn5 setBackgroundImage:image forState:UIControlStateNormal];
            starOneSelected = NO;
        }
        else
        {
            UIImage *image = [UIImage imageNamed:@"star-selected.png"] ;
            [starImageBtn5 setBackgroundImage:image forState:UIControlStateNormal];
            starOneSelected = YES;
        }
    }
     
  7. sandhya.rai thread starter macrumors newbie

    Joined:
    Jan 12, 2009
    #7

    It's not working
     
  8. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #8
    check your IB connections. Dejo's code should work for you. i've attached a working sample if you're really lost.
     

    Attached Files:

  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Not working how? Are you getting any run-time errors? Have you put in breakpoints and stepped through the code to ensure that it is running as expected? It helps if you can give us better details.
     

Share This Page