ios - display data based on uiswitch state -
i trying display data based on if user has selected switch. user have ability select either 1 or 2 switches either both on or 1 or other on. data displayed according state of switch.
i have managed load displaying data dare array in plist file , dosnt matter state switch in displays dare not truth or both.
- (ibaction)shownext:(id)sender { if (!self.plistarray) { nsstring *path = [[nsbundle mainbundle] pathforresource: @"data" oftype:@"plist"]; nsuserdefaults *defaults =[nsuserdefaults standarduserdefaults]; if ([[defaults objectforkey:@"truthonoff"] isequaltostring:@"yes"] && [[defaults objectforkey:@"dareonoff"] isequaltostring:@"yes"] ) { nsdictionary *plistdict1 = [[nsdictionary alloc] initwithcontentsoffile:path]; nsarray * plistarray1 = plistdict1[@"truth"]; nsdictionary *plistdict2 = [[nsdictionary alloc] initwithcontentsoffile:path]; nsarray *plistarray2 = plistdict2[@"dare"]; self.plistarray = [[plistarray1 arraybyaddingobjectsfromarray:plistarray2] mutablecopy]; } else if ([[defaults objectforkey:@"truthonoff"] isequaltostring:@"yes"] ) { nsdictionary *plistdict3 = [[nsdictionary alloc] initwithcontentsoffile:path]; nsarray *plistarray3 = plistdict3[@"truth"] ; self.plistarray = [plistarray3 mutablecopy]; nslog(@"%@", plistarray); } else ([[defaults objectforkey:@"dareonoff"] isequaltostring:@"yes"] ); { nsdictionary *plistdict4 = [[nsdictionary alloc] initwithcontentsoffile:path]; nsmutablearray *plistarray4 = plistdict4[@"dare"]; self.plistarray = [plistarray4 mutablecopy]; nslog(@"%@", plistarray); } } } -(void)stateswitched:(id)sender { uiswitch *tswitch = (uiswitch *)sender; nsuserdefaults *defaults =[nsuserdefaults standarduserdefaults]; [defaults setobject: tswitch.ison ? @"yes" : @"no" forkey:@"truthonoff"]; [defaults synchronize]; } -(void)stateswitcheddare:(id)sender { uiswitch *tswitch = (uiswitch *)sender; nsuserdefaults *defaults =[nsuserdefaults standarduserdefaults]; [defaults setobject: tswitch.ison ? @"yes" : @"no" forkey:@"dareonoff"]; [defaults synchronize]; }
this code setting object
try use boolforkey instead of objectforkey. , in other hand, why else have condition?:
if ([defaults boolforkey:@"truthonoff"] && [defaults boolforkey:@"dareonoff"]) { // both true } else if ([defaults boolforkey:@"truthonoff"] && ![defaults boolforkey:@"dareonoff"]) { // truthonoff true, dareonoff false } else if (![defaults boolforkey:@"truthonoff"] && [defaults boolforkey:@"dareonoff"]) { // truthonoff false, dareonoff true } else { // both false }
make sure 2 things when store boolean values in nsuserstandarddefaults:
- use setbool setter: [[nsuserdefaults standarduserdefaults] setbool:yes forkey:@"a_key"];
- remember synchronize standarduserdefaults save values.
Comments
Post a Comment