【发布时间】:2015-06-29 15:29:51
【问题描述】:
我的问题是我为故事板中的每个视图创建了一个rightBarButtonItem,我的应用程序中的rightBarButtonItem 应该跟踪用户添加到他们的购物清单中的项目的总成本。在我的第一个ViewController 上的viewDidLoad 中,我将rightBarButtonItem's customView 设置为UILabel。当移动到另一个 ViewController 时,我将 ViewController 的 rightBarButtonItem 设置为前一个 ViewController 中的那个。但是,当我回到以前的 ViewController 时,ViewController's rightBarButtonItem 在我尝试更新它时不会改变。相反,它看起来就像我上次移动时所做的那样,如果我再次移动到同一个 ViewController,第一个 ViewController 的rightBarButtonItem 似乎总是比它应该返回的位置落后一步。换句话说,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 - 第二个视图控制器
- (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];
}
【问题讨论】:
-
sendTotalBackFromOrder: 被调用了吗?委托在哪里设置?
-
你可以在 viewWillAppear 而不是 viewDidLoad 中编写这段代码
-
@agy sendTotalBackFromOrder 在我的第二个 ViewController '消失'时被调用。 Delegate 是在第一个 ViewController 的 prepareForSegue 方法中设置的,我只是忘记在描述中添加了。
-
@Rajan 将代码从 viewDidLoad 移动到 viewWillAppear 没有效果。我相信问题是由从第二个 ViewController 返回时的某种堆栈(可能是 rightBarButtonItem 或 navigationItem)引起的
标签: ios objective-c cocoa-touch rightbarbuttonitem