我目前有一个颜色井,可以跟踪保存在 NSUserDefaults 中的颜色。它绑定(bind)到 NSUserDefaultsController。但是,我也想监听颜色的变化,以便我可以相应地更新我的 View 。因此,除了绑定(bind)之外,我还向我的首选项 Controller 添加了一个目标/操作,以发布带有颜色的通知。
1) 同时拥有目标/操作和绑定(bind)有多安全?是否有可能出现滞后或者不同步并报告不同的值?
2)当我在 IBAction 方法中获取颜色时,我应该从用户默认值还是从颜色池中获取它?
这是我的 colorChanged: 操作:
- (IBAction)colorChanged:(id)sender
{
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[colorWell color] forKey:@"color"];
[notificationCenter postNotificationName:@"ColorChangedNotification" object:self userInfo:userInfo];
}
那么我应该这样做:
[NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"color"]];
或者:
[colorWell color];
谢谢!
最佳答案
1) How safe is having both target/action and bindings? Is there a possibility that one might lag or they may be out of sync and report different values?
我认为大多数情况下应该没问题。最好的判断方法就是进行测试。
2) When I am getting the color in my IBAction method, should I get it from the user defaults or from the color well?
您应该绝对,绝对直接从颜色井中获取它。为什么?保存到用户默认值时可能会出现延迟。哎呀,默认值甚至可以在应用程序终止之前仅保存一次,而且仍然没问题。 (好吧,这并不完全正确,但仍然如此)默认值的主要目的是在应用程序启动之间保留数据,而不是在应用程序的生命周期内保留数据。
关于objective-c - 绑定(bind)和目标/操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2211266/