我的問題是,我創建了一個rightBarButtonItem
爲我的故事板中的每個視圖,rightBarButtonItem
在我的應用程序應該跟蹤用戶的項目總成本添加到他們的購物清單。在我的viewDidLoad
的第一個ViewController
上,我將rightBarButtonItem's
customView設置爲UILabel
。當移動到另一個ViewController
時,我將該ViewController的rightBarButtonItem
設置爲前一個ViewController
中的一個。但是,當我回到之前的ViewController
那ViewController's
rightBarButtonItem
不會更改時,我嘗試更新它。相反,它看起來像當我移動前一次時所做的,如果我再次移動到相同的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];
}
是sendTotalBackFromOrder:被稱爲?代表在哪裏設置? – agy
可以在viewWillAppear而不是viewDidLoad –
@agy sendTotalBackFromOrder在我的第二個ViewController'消失'時被調用。 Delegate在第一個ViewController的prepareForSegue方法中設置,我忘記將它添加到描述中。 – thailey01