Objective-C: If statement only runs after second attempt (iOS) -


i've created if statement checks whether bool set true or false

-(void) buttonclicked {      if (sortedlist) {                 // code          sortedlist = no;         [[nsuserdefaults standarduserdefaults] setbool:yes forkey:@"sorted"];     }      else {          // code          sortedlist = yes;         [[nsuserdefaults standarduserdefaults] setbool:no forkey:@"sorted"];     } } 

the bool loaded in viewwillappear

    sortedlist = [[nsuserdefaults standarduserdefaults] boolforkey:@"sorted"]; 

the problem when click button calls buttonclicked, nothing happens. when click second time, correct code gets executed expected (running if or else correctly dependend on how set). third, fourth etc. time button tapped, code executes expected. however, when return view after view has been displayed, problem arrises again no action on first click only.

i've checked whether bool set @ in viewwillappear using log, appears set correctly (with value expect hold).

does have idea missing here?

edit: buttonclicked method gets called follows

[listbutton addtarget:self action:@selector(buttonclicked) forcontrolevents:uicontroleventtouchupinside]; 

edit 2 - fix: i've changed if condition (!sortedlist) , switched assigning of bool in both statements @claudio redi suggested below. fixed me.

you need call

[[nsuserdefaults standarduserdefaults] synchronize]; 

to persist value, after store value using

[[nsuserdefaults standarduserdefaults] setbool:yes forkey:@"sorted"]; 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -