Xcode: Check if UIButton is hidden, using NSUserDe

2019-09-20 13:49发布

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条回答
手持菜刀,她持情操
2楼-- · 2019-09-20 14:38

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];

}
查看更多
登录 后发表回答