gpt4 book ai didi

ios - 如何使用 nsuserdefaults 和 .hidden 保存和加载图片以在另一个 View Controller 上查看

转载 作者:行者123 更新时间:2023-11-29 12:41:56 25 4
gpt4 key购买 nike

我必须在第一个 View Controller 上设置正方形,它在任何时候都是隐藏的,当应用程序启动时,你有两个按钮白色和黑色。当用户按下任一按钮并按下返回查看 1 时,它会显示白色方 block 和黑色方 block ,具体取决于用户选择的方 block 。

代码如下:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidAppear:(BOOL)animated {
NSUserDefaults *SetBlack = [NSUserDefaults standardUserDefaults];
blacksquare.hidden = [SetBlack boolForKey:@"black"];

NSUserDefaults *SetWhite = [NSUserDefaults standardUserDefaults];
whitesquare.hidden = [SetWhite boolForKey:@"white"];
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (IBAction)black:(id)sender{
NSUserDefaults *SetBlack = [NSUserDefaults standardUserDefaults];
[SetBlack setBool:NO forKey:@"black"];
}

- (IBAction)white:(id)sender{
NSUserDefaults *SetWhite = [NSUserDefaults standardUserDefaults];
[SetWhite setBool:NO forKey:@"black"];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end

最佳答案

- (IBAction)black:(id)sender{
NSUserDefaults *SetBlack = [NSUserDefaults standardUserDefaults];
[SetBlack setBool:NO forKey:@"black"];
}

- (IBAction)white:(id)sender{
NSUserDefaults *SetWhite = [NSUserDefaults standardUserDefaults];
[SetWhite setBool:NO forKey:@"black"];
}

上面的代码应该是这样的

 - (IBAction)black:(id)sender{
NSUserDefaults *SetBlack = [NSUserDefaults standardUserDefaults];
[SetBlack setBool:NO forKey:@"black"];
[SetBlack synchronize]; // added synchronize method
}

- (IBAction)white:(id)sender{
NSUserDefaults *SetWhite = [NSUserDefaults standardUserDefaults];
[SetWhite setBool:NO forKey:@"black"];
[SetWhite synchronize]; // added synchronize method
}

来自 NSUserDefaults synchronize 的文档

Because this method is automatically invoked at periodic intervals, use this method only if you cannot wait for the automatic synchronization (for example, if your application is about to exit) or if you want to update the user defaults to what is on disk even though you have not made any changes.

关于ios - 如何使用 nsuserdefaults 和 .hidden 保存和加载图片以在另一个 View Controller 上查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24664742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com