2015-06-29 81 views
0

我的問題是,我創建了一個rightBarButtonItem爲我的故事板中的每個視圖,rightBarButtonItem在我的應用程序應該跟蹤用戶的項目總成本添加到他們的購物清單。在我的viewDidLoad的第一個ViewController上,我將rightBarButtonItem's customView設置爲UILabel。當移動到另一個ViewController時,我將該ViewController的rightBarButtonItem設置爲前一個ViewController中的一個。但是,當我回到之前的ViewControllerViewController'srightBarButtonItem不會更改時,我嘗試更新它。相反,它看起來像當我移動前一次時所做的,如果我再次移動到相同的ViewController,第一個ViewController的rightBarButtonItem似乎總是落後於它應該返回的位置。換句話說,當移動到另一個ViewController時,ViewController的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 - 第二的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'消失'時被調用。 Delegate在第一個ViewController的prepareForSegue方法中設置,我忘記將它添加到描述中。 – thailey01

回答

0

只要改變在viewDidLoad中和sendTotalBackFromOrder行:

[self.navigationItem.rightBarButtonItem setCustomView:customItem]; 

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

self.navigationItem.rightBarButtonItem = barBtn; 
+0

由於@agy像一個魅力一樣工作。我應該想到這一點。 – thailey01

相關問題