【问题标题】:rightBarButtonItem seems to stack when moving from one viewController to another从一个视图控制器移动到另一个视图控制器时,rightBarButtonItem 似乎会堆叠
【发布时间】: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


【解决方案1】:

只需更改 viewDidLoad 和 sendTotalBackFromOrder 中的行:

[self.navigationItem.rightBarButtonItem setCustomView:customItem];

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

self.navigationItem.rightBarButtonItem = barBtn;

【讨论】:

  • 感谢@agy 就像一个魅力。我早该想到的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-18
  • 2019-05-26
  • 2020-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多