我的问题是我rightBarButtonItem
在我的故事板中为每个视图创建了一个,rightBarButtonItem
在我的应用程序中应该跟踪用户添加到他们的购物清单中的项目的总成本。在我viewDidLoad
的第一个中,ViewController
我将rightBarButtonItem's
customView 设置为UILabel
. 当移动到另一个时,ViewController
我将 ViewController 设置rightBarButtonItem
为上一个ViewController
。但是,当我回到上一个时ViewController
,ViewController'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];
}