Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sandhya.rai

macrumors newbie
Original poster
Jan 12, 2009
26
0
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
 
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)?
 
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
 
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.
 
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.



On Button Press there is no effect on the image
 
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;
    }
}
 
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;
    }
}


It's not working
 
check your IB connections. Dejo's code should work for you. i've attached a working sample if you're really lost.
 

Attachments

  • toggleButton.zip
    23.2 KB · Views: 131
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.