0

当开关改变位置以开始更新地图上的注释时,我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];

} 
4

1 回答 1

3

您必须更新一些BOOLean 值以表明您当前没有处理更新,并相应地继续。IE

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

于 2012-06-06T21:39:08.030 回答