ios - How to clear UIButton background image State? -
-(void) btnaction:(id) sender { nsstring *str =((uibutton*)sender).titlelabel.text; nslog(@"%@",str); nslog(@"%@",[dictionary objectforkey:@"option3"]); correctans=[dictionary objectforkey:@"answer"]; if(str==[dictionary objectforkey:@"option1"]) { selectedans=@"1"; if ([selectedans compare:correctans]==nsorderedsame) { nslog(@"this correct"); [cell.ansbtn1 setbackgroundimage:[uiimage imagenamed:@"option_green.png"] forstate:uicontrolstateselected]; countcorrect++; cell.lblrating.text=[nsstring stringwithformat:@"%i", countcorrect]; } else { [cell.ansbtn1 setbackgroundimage:[uiimage imagenamed:@"option_red.png"] forstate:uicontrolstateselected]; cell.lblrating.text=[nsstring stringwithformat:@"%i", countcorrect]; } } }
in initialization of tableview
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"beginingcell"; cell=(beginingcell *)[tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { nsarray *toplevelobjects=[[nsbundle mainbundle] loadnibnamed:@"beginingcell" owner:self options:nil ]; for(id currentobject in toplevelobjects) { if ([currentobject iskindofclass:[beginingcell class]]) { cell=(beginingcell *) currentobject; cell.selectionstyle = uitableviewcellselectionstylenone; break; } } } if(indexpath.row==0) { imageurl=[dictionary objectforkey:@"questionimage"]; // imagename=[dictionary objectforkey:@"questionimage"]; // imageurl=@"http://localhost:85/2/"; // imageurl=[imageurl stringbyappendingstring:imagename]; nsurl *url = [nsurl urlwithstring:imageurl]; nsdata *data = [nsdata datawithcontentsofurl:(nsurl *)url]; uiimage *img = [[uiimage alloc] initwithdata:data]; cell.lblscorecurrent.text=[nsstring stringwithformat:@"%d", reloader]; cell.lblscore.text=@"/30"; cell.myimageview.image = img; cell.sectiontitle.text=[dictionary objectforkey:@"question"]; [cell.ansbtn1 setbackgroundimage:[uiimage imagenamed:@"option.png"] forstate: uicontrolstatenormal]; [cell.ansbtn1 setbackgroundimage:[uiimage imagenamed:@"option_green.png"] forstate: uicontrolstateselected]; [cell.ansbtn1 setbackgroundimage:[uiimage imagenamed:@"option_red.png"] forstate: uicontrolstateselected]; }
with changes code:
this time it's not changing background image option.jpg green/red.it's option.jpg in click.
**and code...i didn't error... **i tested break point it's going through statement properly...even change of image..but can't see backgroud image change.tell me next...
-(void) btnaction:(id) sender { nsstring *str =((uibutton*)sender).titlelabel.text; nslog(@"%@",str); nslog(@"%@",[dictionary objectforkey:@"option3"]); correctans=[dictionary objectforkey:@"answer"]; if([str isequaltostring:([dictionary objectforkey:@"option1"])]) { selectedans=@"1"; if ([selectedans isequaltostring:correctans]) { nslog(@"this correct"); [((uibutton*)sender) setbackgroundimage:[uiimage imagenamed:@"option_green.png"] forstate:uicontrolstateselected]; countcorrect++; cell.lblrating.text=[nsstring stringwithformat:@"%i", countcorrect]; } else { [((uibutton*)sender) setbackgroundimage:[uiimage imagenamed:@"option_green.png"] forstate:uicontrolstateselected]; cell.lblrating.text=[nsstring stringwithformat:@"%i", countcorrect]; } }
all logic ok..even calculating score..i tested it...just image issue not working....please show me way.....i totally new in iphone....learning it..so please give me proper instruction if wrong or bad practice...
after using code :
code button aciton :
-(void) btnaction:(id) sender { ((uibutton *)sender).selected=false; nsstring *str =((uibutton*)sender).titlelabel.text; correctans=[dictionary objectforkey:@"answer"]; if([str isequaltostring:([dictionary objectforkey:@"option1"])]) { selectedans=@"1"; nslog(@"selected ans = %@",selectedans); nslog(@"correct = %@",correctans); if ([selectedans isequaltostring:correctans]) { nslog(@"this correct"); [((uibutton*)sender) setbackgroundimage:[uiimage imagenamed:@"option_green.png"] forstate:uicontrolstateselected]; // sleep(3); countcorrect++; cell.lblrating.text=[nsstring stringwithformat:@"%i", countcorrect]; } else { [((uibutton*)sender) setbackgroundimage:[uiimage imagenamed:@"option_red.png"] forstate:uicontrolstateselected]; cell.lblrating.text=[nsstring stringwithformat:@"%i", countcorrect]; } ((uibutton*)sender).selected = true; }
code in tableview indexpath :
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"beginingcell"; cell=(beginingcell *)[tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { nsarray *toplevelobjects=[[nsbundle mainbundle] loadnibnamed:@"beginingcell" owner:self options:nil ]; for(id currentobject in toplevelobjects) { if ([currentobject iskindofclass:[beginingcell class]]) { cell=(beginingcell *) currentobject; cell.selectionstyle = uitableviewcellselectionstylenone; break; } } } if(indexpath.row==0) { imageurl=[dictionary objectforkey:@"questionimage"]; // imagename=[dictionary objectforkey:@"questionimage"]; // imageurl=@"http://localhost:85/2/"; // imageurl=[imageurl stringbyappendingstring:imagename]; nsurl *url = [nsurl urlwithstring:imageurl]; nsdata *data = [nsdata datawithcontentsofurl:(nsurl *)url]; uiimage *img = [[uiimage alloc] initwithdata:data]; cell.lblscorecurrent.text=[nsstring stringwithformat:@"%d", reloader]; cell.lblscore.text=@"/30"; cell.myimageview.image = img; cell.sectiontitle.text=[dictionary objectforkey:@"question"]; [cell.ansbtn1 setbackgroundimage:[uiimage imagenamed:@"option.png"] forstate: uicontrolstatenormal]; [cell.ansbtn2 setbackgroundimage:[uiimage imagenamed:@"option.png"] forstate: uicontrolstatenormal]; [cell.ansbtn3 setbackgroundimage:[uiimage imagenamed:@"option.png"] forstate: uicontrolstatenormal]; [cell.ansbtn4 setbackgroundimage:[uiimage imagenamed:@"option.png"] forstate: uicontrolstatenormal]; cell.ansbtn1.titlelabel.linebreakmode = uilinebreakmodewordwrap; cell.ansbtn2.titlelabel.linebreakmode = uilinebreakmodewordwrap; cell.ansbtn3.titlelabel.linebreakmode = uilinebreakmodewordwrap; cell.ansbtn4.titlelabel.linebreakmode = uilinebreakmodewordwrap; cell.ansbtn1.titlelabel.textalignment = uitextalignmentcenter; cell.ansbtn2.titlelabel.textalignment = uitextalignmentcenter; cell.ansbtn3.titlelabel.textalignment = uitextalignmentcenter; cell.ansbtn4.titlelabel.textalignment = uitextalignmentcenter; [cell.ansbtn1 settitle:[dictionary objectforkey:@"option1"] forstate:uicontrolstatenormal]; [cell.ansbtn2 settitle:[dictionary objectforkey:@"option2"] forstate:uicontrolstatenormal]; [cell.ansbtn3 settitle:[dictionary objectforkey:@"option3"] forstate:uicontrolstatenormal]; [cell.ansbtn4 settitle:[dictionary objectforkey:@"option4"] forstate:uicontrolstatenormal]; [cell.ansbtn1 addtarget:self action:@selector(btnaction:) forcontrolevents:uicontroleventtouchupinside]; [cell.ansbtn2 addtarget:self action:@selector(btnaction:) forcontrolevents:uicontroleventtouchupinside]; [cell.ansbtn3 addtarget:self action:@selector(btnaction:) forcontrolevents:uicontroleventtouchupinside]; [cell.ansbtn4 addtarget:self action:@selector(btnaction:) forcontrolevents:uicontroleventtouchupinside]; [cell.ansbtn1 addtarget:self action:@selector(btnactionup:) forcontrolevents:uicontroleventtouchdown]; //cell.ansbtn1.showstouchwhenhighlighted=true; cell.ansbtn2.showstouchwhenhighlighted=true; cell.ansbtn3.showstouchwhenhighlighted=true; cell.ansbtn4.showstouchwhenhighlighted=true; } return cell;
}
i still dont understood question..if want 1 normal image button , , when user touches button, can give them directly when create button itself..no need change image when button click..
[cell.ansbtn1 setbackgroundimage:[uiimage imagenamed:@"option.png"] forstate: uicontrolstatenormal]; [cell.ansbtn1 setbackgroundimage:[uiimage imagenamed:@"option_green.png"] forstate: uicontrolstatehighlighted]; [cell.ansbtn2 setbackgroundimage:[uiimage imagenamed:@"option.png"] forstate: uicontrolstatenormal]; [cell.ansbtn2 setbackgroundimage:[uiimage imagenamed:@"option_green.png"] forstate: uicontrolstatehighlighted]; [cell.ansbtn3 setbackgroundimage:[uiimage imagenamed:@"option.png"] forstate: uicontrolstatenormal]; [cell.ansbtn3 setbackgroundimage:[uiimage imagenamed:@"option_green.png"] forstate: uicontrolstatehighlighted]; [cell.ansbtn4 setbackgroundimage:[uiimage imagenamed:@"option.png"] forstate: uicontrolstatenormal]; [cell.ansbtn4 setbackgroundimage:[uiimage imagenamed:@"option_green.png"] forstate: uicontrolstatehighlighted];
since have used highlighted state of button, presume want green image when users hand on button (ie change normal image when user releases key..).in case above code work..
but if want button image remain green after user releases should use selected (uicontrolstateselected) property of uibutton..
edit:after seeing comment..
in case, when button alloc , init give normal state image..
[cell.ansbtn setbackgroundimage:[uiimage imagenamed:@"option.png" forstate:uicontrolstatenormal]];
and on button click.
-(void)optionbuttonclicked:(uibutton*)sender{ if(correct){ // green image.. [sender setbackgroundimage:[uiimage imagenamed:@"option_green.png"] forstate:uicontrolstateselected]; }else{//red image [sender setbackgroundimage:[uiimage imagenamed:@"option_red.png"] forstate:uicontrolstateselected]; } sender.selected = true; }
second edit: after seeing edited question
first thing first, never compare between 2 strings this..
if(str==[dictionary objectforkey:@"option1"])
instead compare using compare() function or isequaltostring() function..
second thing, using "cell" in btnaction function. cant believe not showing "cell undeclared" error in compile time. third, correct above 2 errors. fourth,after correcting error try debug code using breakpoints. have tried already? put breakpoint in btnaction function , see if executing way want..then update question observations..at moment, there errors in code cant tell 1 causing what..to me code shouldnt compiled @ all..
third edit:just afterthought try ..
-(void) btnaction:(id) sender { ((uibutton*)sender).selected = false; nsstring *str =((uibutton*)sender).titlelabel.text; nslog(@"%@",str); nslog(@"%@",[dictionary objectforkey:@"option3"]); correctans=[dictionary objectforkey:@"answer"]; if([str isequaltostring:([dictionary objectforkey:@"option1"])]){ selectedans=@"1"; if ([selectedans compare:correctans]==nsorderedsame) { nslog(@"this correct"); [((uibutton*)sender) setbackgroundimage:[uiimage imagenamed:@"option_green.png"] forstate:uicontrolstateselected]; countcorrect++; // cell.lblrating.text=[nsstring stringwithformat:@"%i", countcorrect]; }else { [((uibutton*)sender) setbackgroundimage:[uiimage imagenamed:@"option_red.png"] forstate:uicontrolstateselected]; cell.lblrating.text=[nsstring stringwithformat:@"%i", countcorrect]; } ((uibutton*)sender).selected = true; } }
fourth edit : 1 more afterthought
-(void) btnaction:(id) sender { nsstring *str =((uibutton*)sender).titlelabel.text; nslog(@"%@",str); nslog(@"%@",[dictionary objectforkey:@"option3"]); correctans=[dictionary objectforkey:@"answer"]; if([str isequaltostring:([dictionary objectforkey:@"option1"])]){ selectedans=@"1"; if ([selectedans compare:correctans]==nsorderedsame) { nslog(@"this correct"); [((uibutton*)sender) setimage:[uiimage imagenamed:@"option_green.png"] forstate:uicontrolstatenormal]; countcorrect++; // cell.lblrating.text=[nsstring stringwithformat:@"%i", countcorrect]; }else { [((uibutton*)sender) setimage:[uiimage imagenamed:@"option_red.png"] forstate:uicontrolstatenormal]; cell.lblrating.text=[nsstring stringwithformat:@"%i", countcorrect]; } } }
Comments
Post a Comment