2

I'm currently displaying a PDF file using the Quick Look framework on an iPad via the Modal View Controller. Works great. My problem is that since I'm displaying a PDF file the Quick Look preview is automatically adding a "Print" button. What I would like to do is replace the "Print" button with a custom "Email" button. Is this something that can be done? At first pass I thought this was going to be a somewhat trivial thing to do but at this point I'm really struggling with it. Any help would be greatly appreciated.

Thanks,

Brett

2 Answers2

1

Since QLPreviewController is a subclass of UIViewController, you can take advantage of -[UIViewController setToolbarItems:] to customize the toolbar.

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(emailPDF)]; 
NSArray *items = [NSArray arrayWithObject:item];
[previewController setToolbarItems:items animated:NO];   
[[self navigationController] presentModalViewController:previewController animated:YES];

Now when the user taps the "reply" icon in the toolbar, your implementation of -emailPDF will get called.

Stephen Poletto
  • 3,645
  • 24
  • 24
  • Thank you Stephen! I can't get this to work. Seems like it should but I must be missing something. I'm new to Objective-C. `QLPreviewController * ql = [[[QLPreviewController alloc] init] autorelease]; ql.dataSource = self; ql.currentPreviewItemIndex = indexPath.row; UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(emailPDF)]; NSArray *items = [NSArray arrayWithObject:item]; [ql setToolbarItems:items animated:NO]; [self presentModalViewController:ql animated:YES];` – Brett Marriner Apr 01 '11 at 00:27
  • Try: [self setToolbarHidden:NO]. UINavigationController's toolbarHidden property is set to YES by default. – Stephen Poletto Apr 01 '11 at 01:30
  • Even with the setToolbarHidden:No I'm not having any luck. I can see the toolbar. It's just not responding to the updated UIBarButtonItem. I wonder if I need to override it somehow? I also wonder if it could be how I'm building the PDF preview. In your code sample I see where you specify '[[self navigationController] presentModalViewController:previewController animated:YES]; '. For me that doesn't work. I can only get it to work if I use '[self presentModalViewController:ql animated:YES];' Am I possibly doing something wrong with how I set the QLPreviewController? – Brett Marriner Apr 01 '11 at 19:53
  • Still trying to wrap my head around this. In your example is previewController a UINavigationController or a UIViewController? I'm sorry this is probably a very "newb" question. Thanks. – Brett Marriner Apr 01 '11 at 23:04
  • The previewController is an instance of QLPreviewController. I was calling this code from the AppDelegate, so I'm calling presentModalViewControler:animated: on the navigationController (An instance of UINavigationController) of the AppDelegate. – Stephen Poletto Apr 01 '11 at 23:12
  • Try downloading Apple's sample code for DocInteraction: developer.apple.com/library/ios/#samplecode/DocInteraction/…. Then, in DTTableViewController, add the following to tableView:didSelectRowAtIndexPath: UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(emailPDF)]; NSArray *items = [NSArray arrayWithObject:item]; [previewController setToolbarItems:items animated:NO]; [item release]; – Stephen Poletto Apr 01 '11 at 23:25
  • Stephen you rock, I'll take a look right now. Thank you again for your help with this. I'll post how it goes. – Brett Marriner Apr 02 '11 at 00:29
  • I'm understanding this better now but I still can't get the "reply" icon in the upper right corner of the PDF preview to call the emailPDF function. I have all of the code together under the didSelectRowAtIndexPath method. Here's the DITableViewControler m file that I've updated. [link](http://www.ceilink.com/transfer/DITableViewController.txt) – Brett Marriner Apr 02 '11 at 01:28
1

you can create a subclass of QLPreviewController like MyQLPreviewController

Then in viewWillAppear:(BOOL)animated (IMPORTANT!!)

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    UIBarButtonItem *rightRatain = self.navigationItem.rightBarButtonItem;
    UIBarButtonItem *email = ...;

    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:right, email, nil];
    [email release];
}
rickytan
  • 110
  • 6