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