我点击时有一组按钮隐藏显示底层图像。我需要知道保存状态的最佳方式,因此在重新启动时不会恢复为未点击的视图。这可能吗?谢谢。隐藏按钮保存
-(IBAction)Act1 {
Act1Button.hidden = YES;
}
-(IBAction)De1 {
Act1Button.hidden = NO;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// initialization
}
return self;
}
- (IBAction)savedata:(id)sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:@"myValue" forKey:@"mykey"];
[defaults synchronize];
}
- (void)viewDidLoad
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if (![userDefaults valueForKey:@"mykey"]) {
[userDefaults setValue:@"myValue" forKey:@"mykey"];
NSLog(@"setting value");
}
[userDefaults synchronize];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
'@interface testViewController : UIViewController
{
IBOutlet UIButton *Act1Button;
IBOutlet UIButton *De1Button;
}
@property (retain, nonatomic) IBOutlet UIButton *Act1Button;
@property (retain, nonatomic) IBOutlet UIButton *De1Button;
- (IBAction)Act1;
- (IBAction)De1;
-(IBAction)savedata:(id)sender;
@end`
我一直在阅读这篇文章,但对我来说这是没有意义的,因为我需要放置代码。你有什么例子吗?我也需要为每个按钮执行此操作吗?再次感谢。 –
每次有人按下按钮时,都会将状态写入NSUserDefaults并执行[[NSUserDefaults standardUserDefaults]同步] ...,将当前用户默认值写入永久内存。当您的应用程序启动时,您只需读取状态并对其执行操作。没有看到您的代码就很难深入其中。希望这可以帮助。 – pho0
我做错了什么。我可以保存并重新加载,如果我按下我的主页按钮并重新启动应用程序,但如果我回到菜单屏幕,我放松了我的更改。我将代码添加到顶部。我真的希望你能帮助我!再次感谢。 –