Search code examples
iosobjective-ccocoa-touchrightbarbuttonitem

rightBarButtonItem seems to stack when moving from one viewController to another


My problem is that I have created a rightBarButtonItem for each view in my storyboard, the rightBarButtonItem in my app is supposed to keep track of the total cost of the items the user has added to their shopping list. In my viewDidLoad on the first ViewController I set the rightBarButtonItem's customView to a UILabel. When moving to another ViewController I set that ViewController's rightBarButtonItem to the one from the previous ViewController. However, when I move back to the previous ViewController that ViewController's rightBarButtonItem doesn't change when I try to update it. Instead, it looks like it did when I moved the previous time, if I move to the same ViewController again the first ViewController's rightBarButtonItem always seems to be one step behind where it should upon return. In other words, the ViewController's rightBarButtonItem seems to stack when moving to another ViewController.

Any help is much appreciated.

-- Relevant code:

viewDidLoad - First ViewController

double total;

- (void)viewDidLoad{
    total = 0;
    NSString *text = [NSString stringWithFormat:@"$%.2f", total];
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
    CGSize constraintSize = CGSizeMake(MAXFLOAT, MAXFLOAT);
    CGSize labelSize = [text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
    UILabel *customItem = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, labelSize.width, labelSize.height)];
    [customItem setText:text];
    [customItem setFont:[UIFont fontWithName:@"Helvetica" size:15.0]];
    [self.navigationItem.rightBarButtonItem setCustomView:customItem];
}

prepareForSegue - First ViewController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    OrderViewController *orderViewController = (OrderViewController *)segue.destinationViewController;
    [orderViewController.navigationItem.rightBarButtonItem setCustomView:_rightBarButtonItem.customView];
}

viewWillDisappear - Second ViewController

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
// sendTotalBackFromOrder is delegate method upon returning to first ViewController
    [_delegate sendTotalBackFromOrder:_total];
    [self removeFromParentViewController];
}

sendTotalBackFromOrder - FirstViewController

// This is the method where it becomes apparent that rightBarButtonItem is being stacked and is always 'behind' where it should be
- (void)sendTotalBackFromOrder:(double)currTotal{
    total = currTotal;
    NSString *text = [NSString stringWithFormat:@"$%.2f", total];
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
    CGSize constraintSize = CGSizeMake(MAXFLOAT, MAXFLOAT);
    CGSize labelSize = [text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
    UILabel *customItem = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, labelSize.width, labelSize.height)];
    [customItem setText:text];
    [customItem setFont:[UIFont fontWithName:@"Helvetica" size:15.0]];
    [self.navigationItem.rightBarButtonItem setCustomView:customItem];
}

Solution

  • Just change the lines in viewDidLoad and sendTotalBackFromOrder:

    [self.navigationItem.rightBarButtonItem setCustomView:customItem];
    

    for

    UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:customItem];
    
    self.navigationItem.rightBarButtonItem = barBtn;