0
我使用NSNotification
在切换开关时发送通知位置开始更新地图上的注释。我的问题是,我有8个开关,当用户更改少数开关的位置时,多次映射更新。如何限制一个通知?有没有办法限制NSNotification?
- (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];
}
如果有人能正确格式化,我会非常感激。我在我的iPhone上。 – CodaFi
你能举一个更新BOOL值的例子吗? –
我做到了。我在代码中将其设置为YES。 – CodaFi