Question

In the storyboard I created a DetailViewController with a UIToolbar at bottom, and one button.

There are special times where I want to present this view controller with an alternative set of buttons.

When I want to present the view controller modally with the extra buttons, I have to initialize the view controller programmatically from the storyboard like so:

DetailViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"Detail"];

[controller newButtons:YES];
[self presentViewController:controller animated:YES completion:nil];

In the DetailViewController.m file I have the method which is called above:

-(void) newButtons: (BOOL) isNew
{
    if (isNew) {
        UIBarButtonItem *doneItem = [[UIBarButtonItem alloc]
                                     initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                     target:self
                                     action:@ selector( save:)];

        UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                       target:self
                                       action:@ selector( cancel:)];

       NSArray *buttonArr = [[NSArray alloc] initWithObjects:doneItem, cancelItem, nil];

    [self.toolbar setItems:buttonArr animated:YES];


    }
}

I have created an outlet from the storyboard to DetailViewController.m file by clicking an dragging to the top of the file which made:

@property (weak, nonatomic) IBOutlet UIToolbar *toolbar; 

These are all the steps but when I click the button to present the view controller, it just shows the view controller with the original button from the Storyboard, not the 2 new ones I programmatically added.

How come it is not showing the new buttons. I logged a test statement from the newButtons method and it is calling it for sure. Just not adding the buttons???

UPDATE:

I added breakpoint to [self.toolbar setItems:animated] and it did not call the statement, even though the method was called. self.toolbar is null.

How come it is null? I cntrl-click dragged to the top of DetailViewController.m and added it. I tried with both strong, and weak properties.

Was it helpful?

Solution 2

Ah I found out the answer! The view controller is not initialized until you present it!

So I just had to switch the order of the code. When I called:

[controller newButtons:YES];

before calling:

[self presentViewController:controller animated:YES completion:nil];

self.toolbar was not initialized.

I had to first present the View Controller then self.toolbar came into existence

[self presentViewController:controller animated:YES completion:nil]; 
[controller newButtons:YES];

Thank you to @Avt & @rmmaddy for helping me debug

OTHER TIPS

Change

@property (weak, nonatomic) IBOutlet UIToolbar *toolbar; 

to

@property (strong, nonatomic) IBOutlet UIToolbar *toolbar; 

Also add

NSLog(@"%@", self.toolbar );

to the newButtons method to check that toolbar is valid.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top