0

我的问题是我rightBarButtonItem在我的故事板中为每个视图创建了一个,rightBarButtonItem在我的应用程序中应该跟踪用户添加到他们的购物清单中的项目的总成本。在我viewDidLoad的第一个中,ViewController我将rightBarButtonItem'scustomView 设置为UILabel. 当移动到另一个时,ViewController我将 ViewController 设置rightBarButtonItem为上一个ViewController。但是,当我回到上一个时ViewControllerViewController's rightBarButtonItem当我尝试更新它时不会改变。相反,它看起来就像我上一次移动时所做的那样,如果我再次移动到同一个 ViewController,第一个 ViewControllerrightBarButtonItem似乎总是比它应该返回的位置落后一步。换句话说,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 - 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];
}
4

1 回答 1

0

只需更改 viewDidLoad 和 sendTotalBackFromOrder 中的行:

[self.navigationItem.rightBarButtonItem setCustomView:customItem];

为了

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

self.navigationItem.rightBarButtonItem = barBtn;
于 2015-06-30T07:17:35.933 回答