0

私の問題はrightBarButtonItem、ストーリーボードに各ビューを作成したrightBarButtonItemことです。アプリでは、ユーザーが買い物リストに追加したアイテムの総コストを追跡することになっています。viewDidLoad最初に、 customView をViewControllerに設定しました。別のものに移動するとき、そのViewControllerを前のものに設定しました。ただし、更新しようとしても変わらない前に戻ると。代わりに、前回移動したときのように見えます。同じ ViewController に再度移動すると、最初の ViewControllerは、戻ったときに常に 1 ステップ遅れているように見えます。つまり、ViewController のrightBarButtonItem'sUILabelViewControllerrightBarButtonItemViewControllerViewControllerViewController's rightBarButtonItemrightBarButtonItemrightBarButtonItem別の に移動するとスタックするよう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 - 2 番目の 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];
}
4

1 に答える 1

0

viewDidLoad と sendTotalBackFromOrder の行を変更するだけです。

[self.navigationItem.rightBarButtonItem setCustomView:customItem];

為に

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

self.navigationItem.rightBarButtonItem = barBtn;
于 2015-06-30T07:17:35.933 に答える