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