2015-06-29 2 views
0

Моя проблема в том, что я создал rightBarButtonItem для каждого вида в своем раскадровке, rightBarButtonItem в моем приложении должен отслеживать общую стоимость предметов, которые пользователь имеет добавлены в их список покупок. В моем viewDidLoad на первом ViewController я установил rightBarButtonItem's customView в UILabel. При переходе на другой номер ViewController я установил rightBarButtonItem ViewController на один из предыдущих ViewController. Однако, когда я возвращаюсь к предыдущему ViewController, ViewController'srightBarButtonItem не изменяется, когда я пытаюсь его обновить. Вместо этого, похоже, что когда я переехал в предыдущий раз, если я снова перейду к тому же ViewController, первый вид ViewController rightBarButtonItem всегда кажется на один шаг ниже, где он должен по возвращении. Другими словами, ViewController, кажется, стекается при переходе на другой 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 - Второй 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]; 
} 
+0

является sendTotalBackFromOrder: называют? где установлен делегат? – agy

+0

Можно ли написать кусок кода в viewWillAppear, а не viewDidLoad –

+0

@agy sendTotalBackFromOrder вызывается, когда мой второй ViewController «исчезает». Делегат установлен в первом методе prepareForSegue ViewController, я просто забыл добавить его в описание. – thailey01

ответ

0

Просто измените строки в viewDidLoad и sendTotalBackFromOrder:

[self.navigationItem.rightBarButtonItem setCustomView:customItem]; 

для

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

self.navigationItem.rightBarButtonItem = barBtn; 
+0

Спасибо, что @agy работает как шарм. Я должен был подумать об этом. – thailey01

Смежные вопросы