我使用NSNotification
在切换位置时发送通知,以开始更新地图上的注释。我的问题是我有8个开关,当用户改变几个开关的位置时,多次映射更新。如何限制一个通知?
- (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];
}
答案 0 :(得分:3)
您必须更新一些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。