ios - the NSUserDefault values not being identified correctly -
ios - the NSUserDefault values not being identified correctly -
in app im trying create buttons of sound , vibration switch between 2 images i.e gray , color depending on whether sound on or not. when clicked should flip on image , go gray color . have managed create flip 1 time color gray doesnt go color when click 1 time again , please help me figure out. in advance. appdelegate.m
if(![defaults objectforkey:@"firsttime"]) { //application running first time //do first time stuff [defaults setbool:no forkey:@"firsttime"]; [defaults setbool:yes forkey:@"musickey"]; [defaults setbool:yes forkey:@"soundkey"]; [defaults setbool:yes forkey:@"vibrationkey"]; [defaults setbool:yes forkey:@"linkkey"]; [defaults synchronize]; } else{ [defaults setbool:yes forkey:@"firsttime"]; }
this settingsviewcontroller actual flipping happen:
-(void)viewdidload { [super viewdidload]; [self checkvalues]; [self makesettingspage]; self.scrollview.contentsize=[_outerview frame].size; } -(void)makesettingspage { roundbutton *about=[[roundbutton alloc]initwithcolor:color1 withtext:@"about"]; roundbutton *privacypolicy=[[roundbutton alloc]initwithcolor:color2 withtext:@"privacy \n policy"]; roundbutton *termsofuse=[[roundbutton alloc]initwithcolor:color2 withtext:@"terms \n of use"]; about.center=cgpointmake(29, 451); privacypolicy.center=cgpointmake(29, 481); termsofuse.center=cgpointmake(29, 511); [self.scrollview setcontentmode:uiviewcontentmodescaleaspectfit]; self.scrollview.frame= self.view.frame; // [scrollview addsubview:viewtoadd]; // scrollview.contentsize = viewtoadd.frame.size; // //content size size of view within scrollview self.containerview.backgroundcolor = [uicolor colorwithpatternimage:[uiimage imagewithcontentsoffile:[[[nsbundle mainbundle] resourcepath] stringbyappendingpathcomponent:@"background_letter.png"]]]; [self.containerview addsubview:about]; [self.containerview addsubview:privacypolicy]; [self.containerview addsubview:termsofuse]; [self.scrollview addsubview:_containerview]; // self.scrollview.contentsize;_containerview.frame.size; [self.outerview addsubview:self.scrollview]; } - (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil { self = [super initwithnibname:nibnameornil bundle:nibbundleornil]; if (self) { } homecoming self; } - (ibaction)musicaction:(id)sender { nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; bool ismusicon=[defaults boolforkey:@"musickey"]; if (!ismusicon) { uiimage *btnimage1 = [uiimage imagenamed:@"musicgrey.png"]; [sender setimage:btnimage1 forstate:uicontrolstatenormal]; } else{ uiimage *btnimage1 = [uiimage imagenamed:@"musiccolor.png"]; [sender setimage:btnimage1 forstate:uicontrolstatenormal]; } } - (ibaction)soundaction:(id)sender { nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; bool isiton=[defaults boolforkey:@"soundkey"]; if (!isiton) { uiimage *btnimage1 = [uiimage imagenamed:@"soundcolors.png"]; [sender setimage:btnimage1 forstate:uicontrolstatenormal]; } else{ uiimage *btnimage1 = [uiimage imagenamed:@"soundgrey.png"]; [sender setimage:btnimage1 forstate:uicontrolstatenormal]; } } - (ibaction)vibrationaction:(id)sender { nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; bool isiton=[defaults boolforkey:@"vibrationkey"]; if (!isiton) { uiimage *btnimage1 = [uiimage imagenamed:@"virationcolor.png"]; [sender setimage:btnimage1 forstate:uicontrolstatenormal]; } else{ uiimage *btnimage1 = [uiimage imagenamed:@"vibrationgrey.png"]; [sender setimage:btnimage1 forstate:uicontrolstatenormal]; } } - (ibaction)pushaction:(id)sender { } - (ibaction)linkaction:(id)sender { nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; bool isiton=[defaults boolforkey:@"linkkey"]; if (!isiton) { uiimage *btnimage1 = [uiimage imagenamed:@"colorfulfacebook.png"]; [sender setimage:btnimage1 forstate:uicontrolstatenormal]; } else{ uiimage *btnimage1 = [uiimage imagenamed:@"grayfacebook.png"]; [sender setimage:btnimage1 forstate:uicontrolstatenormal]; } } -(void)checkvalues { nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; if([defaults boolforkey:@"musickey"]) { //load active image [self.musicbutton setimage:[uiimage imagenamed:@"musiccolor.png"] forstate:uicontrolstatenormal]; } else{ //load grayness image [self.musicbutton setimage:[uiimage imagenamed:@"musicgrey.png"] forstate:uicontrolstatenormal]; } if([defaults boolforkey:@"soundkey"]) { //load active image [self.soundbutton setimage:[uiimage imagenamed:@"soundcolors.png"] forstate:uicontrolstatenormal]; } else{ //load grayness image [self.soundbutton setimage:[uiimage imagenamed:@"soundgrey.png"] forstate:uicontrolstatenormal]; } if([defaults boolforkey:@"vibrationkey"]) { //load active image [self.vibrationbutton setimage:[uiimage imagenamed:@"virationcolor.png"] forstate:uicontrolstatenormal]; } else{ //load grayness image [self.vibrationbutton setimage:[uiimage imagenamed:@"vibrationgrey.png"] forstate:uicontrolstatenormal]; } if([defaults boolforkey:@"linkkey"]) { //load active image [self.linkbutton setimage:[uiimage imagenamed:@"colorfulfacebook.png"] forstate:uicontrolstatenormal]; } else{ //load grayness image [self.soundbutton setimage:[uiimage imagenamed:@"grayfacebook.png"] forstate:uicontrolstatenormal]; } } @end
for 1 thing, not how set default defaults:
if(![defaults objectforkey:@"firsttime"])
instead, phone call registerdefaults:
. that's it's for.
ios objective-c nsuserdefaults
Comments
Post a Comment