我的问题是我在故事板中为每个视图创建了rightBarButtonItem
,我应用中的rightBarButtonItem
应该跟踪用户添加到其中的项目的总费用购物清单。在第一个viewDidLoad
的{{1}}我将ViewController
customView设置为rightBarButtonItem's
。当移动到另一个UILabel
时,我将ViewController的ViewController
设置为前一个rightBarButtonItem
的那个。但是,当我回到上一个ViewController
时,{I}} ViewController
在我尝试更新时没有改变。相反,它看起来就像我上一次移动时所做的那样,如果我再次移动到同一个ViewController,第一个ViewController的ViewController's
似乎总是落后它应该返回的一步。换句话说,ViewController的rightBarButtonItem
似乎在移动到另一个rightBarButtonItem
时堆叠。
非常感谢任何帮助。
- 相关代码:
viewDidLoad - First ViewController
rightBarButtonItem
prepareForSegue - First ViewController
ViewController
viewWillDisappear - 第二个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];
}
sendTotalBackFromOrder - FirstViewController
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
OrderViewController *orderViewController = (OrderViewController *)segue.destinationViewController;
[orderViewController.navigationItem.rightBarButtonItem setCustomView:_rightBarButtonItem.customView];
}
答案 0 :(得分:0)
只需更改viewDidLoad和sendTotalBackFromOrder中的行:
[self.navigationItem.rightBarButtonItem setCustomView:customItem];
的
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:customItem];
self.navigationItem.rightBarButtonItem = barBtn;