Skip to content

Instantly share code, notes, and snippets.

@odrobnik
Created October 11, 2012 11:55
Show Gist options
  • Save odrobnik/3871852 to your computer and use it in GitHub Desktop.
Save odrobnik/3871852 to your computer and use it in GitHub Desktop.
#pragma mark - Undo Support
- (void)addUndoObservers
{
for (CatalogPage *onePage in _catalog.pages)
{
NSArray *undoableHotZoneKeys = [CatalogPageHotZone undoableKeys];
for (CatalogPageHotZone *oneHotZone in onePage.hotZones)
{
for (NSString *oneKey in undoableHotZoneKeys)
{
[oneHotZone addObserver:self forKeyPath:oneKey options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:DTCatalogDocumentUndoObservationContext];
}
}
}
}
- (void)removeUndoObservers
{
for (CatalogPage *onePage in _catalog.pages)
{
NSArray *undoableHotZoneKeys = [CatalogPageHotZone undoableKeys];
for (CatalogPageHotZone *oneHotZone in onePage.hotZones)
{
for (NSString *oneKey in undoableHotZoneKeys)
{
[oneHotZone removeObserver:self forKeyPath:oneKey context:DTCatalogDocumentUndoObservationContext];
}
}
}
}
- (void)observeForUndoValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change
{
NSUndoManager *undoManager = [self undoManager];
id oldValue = change[NSKeyValueChangeOldKey];
if (oldValue == [NSNull null])
{
oldValue = nil;
}
[[undoManager prepareWithInvocationTarget:object] setValue:oldValue forKeyPath:keyPath];
[undoManager setActionName:NSLocalizedString(@"Edit Hot Zone", @"Edit Action")];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (context == DTCatalogDocumentUndoObservationContext)
{
[self observeForUndoValueForKeyPath:keyPath ofObject:object change:change];
return;
}
if ([keyPath isEqualToString:@"selectionIndexes"])
{
CatalogPage *page = _catalog.pages[_arrayController.selectionIndex];
_inspectorTabViewController.representedObject = page;
// cannot animate or else we get exception on first swipe backwards
[_pageController setSelectedIndex:_arrayController.selectionIndex];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment