0

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];
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
thailey01
  • 165
  • 1
  • 14
  • is sendTotalBackFromOrder: being called? where the delegate is set? – agy Jun 29 '15 at 16:56
  • can u write the piece of code in viewWillAppear rather than viewDidLoad – Rajan Maheshwari Jun 29 '15 at 18:52
  • @agy sendTotalBackFromOrder is called when my second ViewController 'disappears'. Delegate is set in the first ViewController's prepareForSegue method, I just forgot to add it in the description. – thailey01 Jun 29 '15 at 22:28
  • @Rajan moving code from viewDidLoad to viewWillAppear had no effect. I believe the problem is caused by some kind of stack (possibly rightBarButtonItem or navigationItem) upon return from second ViewController – thailey01 Jun 29 '15 at 22:32

1 Answers1

0

Just change the lines in viewDidLoad and sendTotalBackFromOrder:

[self.navigationItem.rightBarButtonItem setCustomView:customItem];

for

UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:customItem];

self.navigationItem.rightBarButtonItem = barBtn;
agy
  • 2,804
  • 2
  • 15
  • 22