2012-06-06 74 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

相關問題