使用 NSNotifications 来做到这一点。
由于第三个选项卡是您的配置设置,因此您可能希望将这些设置存储在其中,NSUserDefaults
因此请使用NSUserDefaultsDidChangeNotification
来在您的viewDidLoad
方法中注意这一点,并将您的 reloadData 代码移动到它自己的方法中。
- (void)viewDidLoad
{
[super viewDidLoad];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(userDefaultsChanged:)
name:NSUserDefaultsDidChangeNotification
object:nil];
[self reloadData];
}
现在,只要您的默认值发生更改,这将触发对该方法的调用,userDefaultsChanged:
请按如下方式添加该方法。
- (void)userDefaultsChanged:(NSNotification *)notification
{
[self reloadData];
}
- (void)viewDidUnload
{
[super viewDidUnLoad];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
编辑:查看特定默认值的替代方法
[[NSUserDefaults standardUserDefaults] addObserver:self
forKeyPath:@"SomeDefaultKey"
options:NSKeyValueObservingOptionNew
context:NULL];
- (void)observeValueForKeyPath:(NSString *) keyPath ofObject:(id) object change:(NSDictionary *) change context:(void *) context
{
if([keyPath isEqual:@"SomeDefaultKey"])
{
// Do Something
}
if([keyPath isEqual:@"SomeOtherKey"])
{
// Do Something else
}
}