Search code examples
objective-cioscocoa-touchuibarbuttonitemqlpreviewcontroller

How to add UIBarButtonItem to QLPreviewController


I already read this question QLPreviewController remove or add UIBarButtonItems but it's not what I'm looking for. I would like to keep the "Print" button in the navigation Bar but also add a new "Delete Document" button in the navigation bar.

I tried this:

QLPreviewController *previewer = [[[QLPreviewController alloc] init] autorelease];
[previewer setDataSource:self];

UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]initWithTitle:@"Salva Documento" style:UIBarButtonItemStyleBordered target:self action:@selector(saveFileToDocuments)];
NSArray *buttons = [NSArray arrayWithObjects:[[previewer navigationItem]rightBarButtonItem],saveButton, nil];
[[previewer navigationItem]setRightBarButtonItems:buttons];

But it didn't work.


Solution

  • Because you said "4.x will be fine", there's your problem.

    The documentation for UINavigationItem [setRightBarButtonItems: animated:] (documentatin linked there for you) say that this function only works on iOS 5.0 and newer.

    It will not work on iOS 4.0.

    Also, you should add an animated: parameter to that setRightBarButtonItems: call.