yoyo
yoyo

Reputation: 71

how to add a button into QLPreviewController

Is it possible to add a new button into the toolBar or into the actionButton on the right top of QLpreviewController?

If yes, how i write the code?

Upvotes: 3

Views: 4422

Answers (2)

brady
brady

Reputation: 132

Try setting the rightBarButtonItem in viewDidLoad of your custom QLPreviewController class, like this:

import QuickLook
import UIKit

class FileViewController: QLPreviewController {
     override func viewDidLoad() {
          super.viewDidLoad()
          navigationItem.rightBarButtonItem = yourButton
     }
}

Upvotes: 0

memmons
memmons

Reputation: 40502

Although QLPreviewController is a subclass of UIViewController changing navigation items has no effect. For example, this code should theoretically work but doesn't:

QLPreviewController *previewController = [[QLPreviewController alloc] init];
previewController.navigationItem.rightBarButtonItem = 
   [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction 
                             target:self action:@selector(share)] 

You can, however, add a toolbar to a QLPreviewController. First set up your toolbar to show when the view is loaded:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.toolbarHidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.toolbarHidden = YES;
}

Next, set your toolbarItems from the QLPreviewController delegate:

- (id)previewController:(QLPreviewController *)previewController 
                 previewItemAtIndex:(NSInteger)idx {

UIBarButtonItem *testButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Test"
                                                 style:UIBarButtonItemStylePlain
                                                 target:self
                                                 action:@selector(testButtonTapped:)];

NSArray *myToolbarItems = [NSArray arrayWithObjects:testButtonItem, nil];
previewController.toolbarItems = myToolbarItems;
[testButtonItem release];
}

Upvotes: 1

Related Questions