从一个 viewController 移动到另一个时,rightBarButtonItem 似乎堆叠

rightBarButtonItem seems to stack when moving from one viewController to another

我的问题是我为故事板中的每个视图创建了一个 rightBarButtonItem,我的应用程序中的 rightBarButtonItem 应该跟踪用户添加的项目的总成本到他们的购物清单。在第一个 ViewControllerviewDidLoad 中,我将 rightBarButtonItem's customView 设置为 UILabel。移动到另一个 ViewController 时,我将 ViewController 的 rightBarButtonItem 设置为前一个 ViewControllerViewController。但是,当我回到以前的 ViewController 时, ViewController's rightBarButtonItem 在我尝试更新它时不会改变。相反,它看起来就像我上次移动时那样,如果我再次移动到相同的 ViewController,第一个 ViewController 的 rightBarButtonItem 似乎总是落后它应该的位置一步在 return 之后。换句话说,ViewController 的 rightBarButtonItem 似乎在移动到另一个 ViewController 时堆叠。

非常感谢任何帮助。

-- 相关代码:

viewDidLoad - 第一个 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 - 第一个 ViewController

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

viewWillDisappear - 第二个 ViewController

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

sendTotalBackFromOrder - 第一个ViewController

// 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];
}

只需更改 viewDidLoad 和 sendTotalBackFromOrder 中的行:

[self.navigationItem.rightBarButtonItem setCustomView:customItem];

为了

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

self.navigationItem.rightBarButtonItem = barBtn;