How to add UIBarButtonItem to QLPreviewController

2.3k views Asked by At

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.

2

There are 2 answers

3
Michael Dautermann On BEST ANSWER

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.

0
Sanjeev Rao On
    UIBarButtonItem *rbb;
-(void)addRightButton{
    if (!rbb) {
        UIButton *orderButton = [UIButton buttonWithType:UIButtonTypeCustom];
        orderButton.frame = CGRectZero;
        rbb = [[UIBarButtonItem alloc] initWithCustomView:orderButton];
    }

    self.navigationItem.rightBarButtonItem = rbb;
}

- (void)viewDidLoad{
    [super viewDidLoad];
    [self performSelector:@selector(addRightButton) withObject:nil afterDelay:0.2];
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self addRightButton];

}