2012-06-06 75 views
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]; 

} 

回答

3

您必须更新一些BOOL EAN值,表明你是不是正在处理的最新情况,并进行相应处理。 I.E.

- (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。

+0

如果有人能正确格式化,我会非常感激。我在我的iPhone上。 – CodaFi

+0

你能举一个更新BOOL值的例子吗? –

+0

我做到了。我在代码中将其设置为YES。 – CodaFi

相关问题