0

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.

Community
  • 1
  • 1
Lolloz89
  • 2,809
  • 2
  • 26
  • 41

2 Answers2

0
    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];

}
Sanjeev Rao
  • 2,247
  • 1
  • 19
  • 18
0

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.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Use the debugger and step through each line of code and see if a `nil` object is being referenced. For example, I see multiple references to `[previewer navigationItem]`. Assign that to a variable via `UINavigationItem * previewerNavigationItem = [previewer navigationItem];` and then see if it's nil. Also, check if `[previewer navigationItem]rightBarButtonItem]` is returning nil also. – Michael Dautermann Jan 13 '12 at 14:34
  • previewerNavigationItem is not nil, but almost everything inside it, is 0x0 (like title, left and rightBarButtonItem)... Is it possible that those object are loaded when the controller is pushed into the stack? If so, how should I add that button to the navigation bar? – Lolloz89 Jan 13 '12 at 14:56
  • 3
    Yes... you are on the right track. Push the controller onto the stack *first* and then do your customizations. – Michael Dautermann Jan 13 '12 at 14:58