作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 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];
}
最佳答案
您必须更新一些 BOOL
ean 值以指示您当前未在处理更新,并相应地继续。即
- (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/
我是一名优秀的程序员,十分优秀!