2011-06-14 58 views
2

由於this question [種類]問,我如何在我的MainWindow.xib中設置一些工具欄項目,並在使用UINavigationControllerpushViewController:animated:時使這些項目存在。如何在iOS視圖中保留導航工具欄

例如,我的應用程序有:

  • MainWindow.xib,其中包含與導航欄和工具欄一UINavigationController
  • AViewController.xib,其中只包含UITableView

在某一點上,我們的用戶按下工具欄按鈕和相關操作執行:

- (void)someAction { 
    [self.navigationController pushViewController:[[AViewController alloc] 
     initWithNibName:nil bundle:nil] 
     animated:YES]; 
} 

當新的觀點被推開,它包含了一個空白欄,而不是工具欄具有相同的項目像之前一樣。讓他們保持一致的首選方法是什麼?我覺得我只是想念一些簡單的東西!提前致謝!

+0

我已經想通了解決的辦法,但我有興趣,如果別人有投入,所以我會等待回答它。 – Rob 2011-06-15 14:11:04

回答

2

我有我的一個類採用UINavigationControllerDelegate協議並實現navigationController:willShowViewController:animated:方法就是這樣,

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { 
    [viewController setToolbarItems:toolbarItems animated:NO]; 
} 

,我創建並存儲在toolbarItems的委託類。作爲委託人的可能候選人之一是應用程序委託類,因爲它是實例化導航控制器的最可能的位置。

+0

非常酷。在我的應用程序委託和上面的函數中添加了''給這個委託代碼。普雷斯托。 只有我需要的其他技巧,因爲我指定'.xib'中的項目是在'navigationController:willShowViewController:animated'中放置if語句以確定工具欄是否包含項目。當它第一次被調用時,它不會將工具欄清空。 – Rob 2011-06-16 16:18:35

0

我已經結束了切換到Deepak的答案,但我也認爲我會提供這種情況下,以幫助某人(或如果人們想提供反饋)。

在我showAction上述方法定義的,我把它重構爲這樣:

- (void)someAction { 
    AViewController *av = [[AViewController alloc] 
     initWithNibName:nil bundle:nil]; 
    [av setToolbarItems:[[self.navigationController toolbar] items]]; 
    [self.navigationController pushViewController:av animated:YES]; 
}