I'm trying to use Storyboards and to reduce coupling between my classes using KVC. Like the Contacts app, I have editable fields that push new view controllers where you can edit data. Before I show one of my detailViewControllers, I do things like this
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"ShowDetailMap"]) {
UIViewController *destination = ((UINavigationController *)segue.destinationViewController).topViewController;
if ([destination respondsToSelector:@selector(setMapTypeAsNum:)]) {
[destination setValue:[NSNumber numberWithInteger:self.mapView.mapType] forKey:@"mapTypeAsNum"];
[destination addObserver:self forKeyPath:@"mapTypeAsNum" options:NSKeyValueObservingOptionNew context:NULL];
}
How do you removeObserver and maintain loose coupling between classes? I can conform my viewController class to the UINavigationControllerDelegate protocol, but it seems bad to do something like:
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if ([viewController respondsToSelector:@selector(setNewmap:)]) {
if ([[navigationController topViewController] isKindOfClass:[AddMapViewController class]]) {
// remove observer
}
}
}
It seems like there should be a better way to removeObserver than checking if the DetailViewController is of a certain type and remove that observer for that view controller. What would you reocommend? Thanks.