gpt4 book ai didi

ios - 有没有办法限制 NSNotification?

转载 作者:行者123 更新时间:2023-11-28 20:34:54 25 4
gpt4 key购买 nike

我使用 NSNotification 在开关改变位置时发送通知以开始更新 map 上的注释。我的问题是我有 8 个开关,当用户改变几个开关的位置时, map 会更新很多次。如何将其限制为仅一个通知?

- (IBAction)saveSwitch:(id)sender
{

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyAppSettingsChanged" object:self userInfo:nil];


NSUserDefaults *defs1 = [NSUserDefaults standardUserDefaults];
[defs1 setBool: blackSwitch.on forKey: @"blackKey"];

NSUserDefaults *defs2 = [NSUserDefaults standardUserDefaults];
[defs2 setBool: greenSwitch.on forKey: @"greenKey"];

NSUserDefaults *defs3 = [NSUserDefaults standardUserDefaults];
[defs3 setBool: purpleSwitch.on forKey: @"purpleKey"];

NSUserDefaults *defs4 = [NSUserDefaults standardUserDefaults];
[defs4 setBool: orangeSwitch.on forKey: @"orangeKey"];

NSUserDefaults *defs5 = [NSUserDefaults standardUserDefaults];
[defs5 setBool: blueSwitch.on forKey: @"blueKey"];

NSUserDefaults *defs6 = [NSUserDefaults standardUserDefaults];
[defs6 setBool: redSwitch.on forKey: @"redKey"];

NSUserDefaults *defs7 = [NSUserDefaults standardUserDefaults];
[defs7 setBool: darkblueSwitch.on forKey: @"darkblueKey"];

NSUserDefaults *defs8 = [NSUserDefaults standardUserDefaults];
[defs8 setBool: yellowSwitch.on forKey: @"yellowKey"];

[[NSUserDefaults standardUserDefaults] synchronize];

}

另一种观点

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppSettingsChanged:) name:@"MyAppSettingsChanged" object:nil];

}

更新注释

- (void) onAppSettingsChanged:(NSNotification *)notification {

NSLog(@"Settings was changed");

//Create the thread
[NSThread detachNewThreadSelector:@selector(loadPList) toTarget:self withObject:nil];

}

最佳答案

您必须更新一些 BOOLean 值以指示您当前未在处理更新,并相应地继续。即

- (void) onAppSettingsChanged:(NSNotification *)notification {
  NSLog(@"Settings was changed");
if (!myBoolToIndicateProcessing) {
  //Create the thread
myBoolToIndicateProcessing = YES;
  [NSThread detachNewThreadSelector:@selector(loadPList) toTarget:self withObject:nil];
}
}

然后,当重新加载完成后,将 BOOL 设置回 NO。

关于ios - 有没有办法限制 NSNotification?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10922628/

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