rightBarButtonItem似乎在从一个viewController移动到另一个viewController时堆叠

时间:2015-06-29 15:29:52

标签: ios objective-c cocoa-touch rightbarbuttonitem

我的问题是我在故事板中为每个视图创建了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];
}

1 个答案:

答案 0 :(得分:0)

只需更改viewDidLoad和sendTotalBackFromOrder中的行:

[self.navigationItem.rightBarButtonItem setCustomView:customItem];

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

self.navigationItem.rightBarButtonItem = barBtn;