5

I have an object that need to be notified when a QLPreviewController changes the shown document. QLPreviewController have the property currentPreviewItemIndex that is updated when the document change. I've added my object as observer for currentPreviewItemIndex and it receives the notification when in my code is changed the property, so far so good. The problem is that the user can change the shown document swiping in the screen and I've found out that in this case the notification isn't generated.

Any solution to receive the notification also in this case? I suppose that the notification is generated when is called the setter of the property currentPreviewItemIndex and probably when the user swipe the property is changed internally in the object QLPreviewController.

Another solution may be to disable the horizontal swipe in QLPreviewController but preserving the vertical swipe (there are the arrows buttons to change the shown document). How do you do that?

Thanks in advance for the help.

Giannandrea

jean71
  • 143
  • 6
  • Have you fond any solution for this problem? I also facing similar issue. If you got solution then can you please guide here? – Vijay Mar 12 '14 at 06:08

2 Answers2

1

make a category on the QLPreviewController and swizzle the appropriate method and either add the willChange/didChange for KVO ;)

seriously though: I tried KVO and it didnt work for me either.. 1) id file a bug with apple for that saying you need this

BUT as a workaround

  • (id )previewPanel:(QLPreviewPanel *)panel previewItemAtIndex:(NSInteger)index {

this is called ok and everytime we swipe so I would 'hack' this to FIRE your own correct KVO. something like

     static NSInteger oldIndex = -1; //reset when the panel is hidden or shown
     int newIndex = qlController.displayedIndex;
     if(oldIndex != newIndex) {
         oldIndex = newIndex;
         [qlController willChangeValueForKey:@"displayedIndex"];
         [qlController didChangeValueForKey:@"displayedIndex"];
     }

I wrote it inline here so there are bound to be typos and mistakes but I think the general approach could work.

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
1

//1. Declare a static context:

static void *changePageContext = &changePageContext;

//2. In viewDidLoad add self as observer for currentPreviewItemIndex property of a strong ref to your QLPreviewController:

[self.previewController addObserver:self forKeyPath:@"currentPreviewItemIndex" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:changePageContext];

//3. Implement the observer method:

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
    change:(NSDictionary *)change context:(void *)context
{
    if (context == changePageContext)
    {
        NSLog(@"newValue:%ld",(long)self.previewController.currentPreviewItemIndex);
    }
    else
    {
        // Any unrecognized context must belong to super
        [super observeValueForKeyPath:keyPath
                             ofObject:object
                               change:change
                              context:context];
    }
}

//4. Remove the observer in viewWillDisappear:

-(void)viewWillDisappear:(BOOL)animated
{
    if (![[self.navigationController viewControllers] containsObject: self])
    {
        [self.previewController removeObserver:self forKeyPath:@"currentPreviewItemIndex"];
    }
}