Xcode: Check if UIButton is hidden, using NSUserDefaults

a 夏天 提交于 2019-12-13 09:36:04

问题


How would I allow the app to save & load the current state of a button (i.e. if it is hidden, how would I save this, so that next time it will be hidden)? Also, I have it currently set so that when a button is pressed, all the other buttons become un-hidden. How can I set it so that certain ones don't become un-hidden if they have been set un-hidden in NSUserDefaults if you see what I mean?

Thanks


回答1:


You can check with Apple's documentation on NSUserDefaults to get any additional information you need.

 NSUserDefaults *defaults = [NSUserDefaults standardDefaults];
 [defaults setObject:myBool boolForKey:@"hiddenButton"];
 [defaults synchronize];

Then you can just pull it in the same way and set your hidden value.

NSUserDefaults *defaults = [NSUserDefaults standardDefaults];
myButton.hidden = [defaults boolForKey:@"hiddenButton"];

EDIT:

This would be one way to implement, but it really depends on what you want.

myViewController.h

BOOL myButtonState;


myViewController.m

-(void)viewDidLoad {

   NSUserDefaults *defaults = [NSUserDefaults standardDefaults];
   myButton.hidden = [defaults boolForKey:@"hiddenButton"];

}

-(IBAction)buttonPressed {

  myButtonState = TRUE;
  NSUserDefaults *defaults = [NSUserDefaults standardDefaults];
  [defaults setObject:myButtonState boolForKey:@"hiddenButton"]; 
  [defaults synchronize];

}


来源:https://stackoverflow.com/questions/7322442/xcode-check-if-uibutton-is-hidden-using-nsuserdefaults

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!