Megasaur
Megasaur

Reputation: 626

UIBarButtonItems cannot be removed from one toolbar and added to another?

I have a UISplitViewController. I have multiple detail views. The detail views have toolbars. I want to move the UIBarButtonItem provided from the UISplitViewControllerDelegate between toolbars.

So I save a references to the UIBarButtonItem, and when I swap views, I remove it from the current detail view and move it to the new detail view.

The UIBarButtonItem works exactly once. If I start with view controller A in the UISplitViewController detail pane, the item is displayed in the toolbar. When I switch to view controller B, I can see the item being removed and added and it takes up space, but it never displays in the new toolbar.

I can solve this by copying the UIBarButtonItem every time I want to add it to a new toolbar. I would really just like to use the saved value.

What's going on?

Code

My app delegate is also my UISplitViewControllerDelegate. All my detail views also conform to UISplitViewControllerDelegate.

// Save
- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc
{
    /* Omitted code that calls the same method on the current detail view. */
    // Save the item
    self.savedBarButtonItem = barButtonItem;
}

I have an IBAction in my app delegate:

-(IBAction)changeDetailView:(id)sender
{
    /* omitted code to the the new view controller and the current view controller */
    [currentView removeBarButtonItem];

    //This adds the item but the item does not even show up.
    [newView addBarButtonItem:self.savedBarButtonItem];

    // New item with the same target and action works.
    UIBarButtonItem * newItem = 
        [[UIBarButtonItem alloc] initWithTitle:@"Test" style:UIBarButtonItemStyleBordered target:self.savedBarButtonItem.target action:self.savedBarButtonItem.action];
    [newView addBarButtonItem:newItem];

}

And the way I add and remove the UIBarButtonItems in the detail view controllers:

-(void)addBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    NSArray * items = self.toolbar.items;
    NSMutableArray * newArr = [NSMutableArray arrayWithCapacity:[items count]+1];
    [newArr addObject:barButtonItem];
    for(NSObject * o in items)
        [newArr addObject:o];
    [self.toolbar setItems:newArr animated:YES];
}

-(void)removeBarButtonItem
{
    NSArray * items = self.toolbar.items;
    NSMutableArray * newArr = [NSMutableArray arrayWithCapacity:[items count]-1];
    for(NSInteger i=1; i<[items count]; i++)
        [newArr addObject:[items objectAtIndex:i]];
    [self.toolbar setItems:newArr animated:YES];
}

Upvotes: 1

Views: 299

Answers (1)

David Hoerl
David Hoerl

Reputation: 41642

I suspect the issue is due to the use of animation in the remove method. The toolbar most likely keeps references to the old times array around while its animating them out of the view. So what I suggest is that you try changing animated: to NO in the remove, and see if that fixes it. Perhaps try it in both. If it then works then this theory is right.

You never mentioned if this item uses a customView or not, which might be more problematic.

Upvotes: 1

Related Questions