1

我在導航工具欄中遇到問題。我在我的應用程序委託中設置了導航工具欄背景圖像,然後有一個按鈕,單擊它時會更改導航工具欄背景圖像並移動到另一個視圖。彈出視圖時未恢復自定義導航欄背景

但是,當我點擊後退按鈕並返回到原始視圖時,導航工具欄背景圖像不會改回。

下面是相關代碼:

在我的第一個視圖控制器:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    //set to change the navigation toolbar to this background image (the first) when this view loads 
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"QuickGrocery Logo Updated.jpg"] forBarMetrics:UIBarMetricsDefault]; 
} 

按鈕動作:

- (IBAction)wellBalancedMealsClicked:(id)sender { 

    QuickGroceryMasterViewController *masterViewController = [[QuickGroceryMasterViewController alloc] initWithNibName:@"QuickGroceryMasterViewController" bundle:nil]; 

    [masterViewController setRecipeChoice:1]; 

//changes the navigation toolbar background image 
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"Keep It Balanced.jpg"] forBarMetrics:UIBarMetricsDefault]; 
} 
+0

你的代碼是不工作的原因是'回到你的第一個視圖控制器時viewDidLoad'不叫。只有從視圖控制器的視圖加載視圖控制器的視圖時,纔會調用此方法。 Rob Mayoff的回答是最好的解決方案。 – jrturton

回答

2

有導航欄只是一個圖像。如果你改變它,並且以後你想讓它改回來,你必須自己改變它。

我建議你給每個視圖控制器一個navigationBarImage屬性。爲您的導航控制器設置代表。在委託中,添加這個方法:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated 
{ 
    UIImage *image = [(id)viewController navigationBarImage]; 
    if (image) 
     [navigationController.navigationBar setBackgroundImage:image forBarMetrics: UIBarMetricsDefault]; 
} 
+0

羅布,感謝您的幫助。如何爲導航控制器「設置委託人」? – user1072337

+0

將導航控制器的'delegate'屬性設置爲實現'navigationController:willShowViewController:'的類的實例。 –

+0

[可可核心競爭力:授權](http://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html) –