2009-11-24 50 views
10

因此,推視圖控制器我的tableView,如何在推入另一個視圖控制器後將按鈕添加到我的navigationController的右側?

 
// Override to support row selection in the table view. 
- (void)tableView:(UITableView *)tableView 
     didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Navigation logic may go here -- 
    // for example, create and push another view controller. 
    AnotherViewController *anotherViewController = 
     [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil]; 
    [self.navigationController pushViewController:anotherViewController animated:YES]; 

好了,這樣就使得另一觀看幻燈片,您可以通過點擊返回到上一個視圖(「彈出」當前視圖)後立即自動現在出現在導航欄的左上角。

好的,所以說我想用DONE按鈕填充導航欄的右側,就像在iPhone附帶的「Notes」應用程序中一樣。我會怎麼做?

我想這樣的代碼:

 
    UIBarButtonItem * doneButton = 
    [[UIBarButtonItem alloc] 
    initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
    target:self 
    action:@selector(doneFunc) ]; 

    self.navigationController.navigationItem.rightBarButtonItem = doneButton ; // not it.. 

    [doneButton release] ; 

doneFunc定義,和一切,只是按鈕永遠不會出現在右側..

回答

11

啊。你必須這樣做:

 
- (void)tableView:(UITableView *)tableView 
     didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    AnotherViewController *anotherViewController = 
     [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil]; 
    [self.navigationController pushViewController:anotherViewController animated:YES]; 


    UIBarButtonItem * doneButton = 
    [[UIBarButtonItem alloc] 
    initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
    target:self 
    action:@selector(doneFunc) ]; 

    anotherViewController.navigationItem.rightBarButtonItem = doneButton ; 

    [doneButton release] ; 
} 

who'da thunk it。

+14

你應該已經接受了托馬斯的答案,而不是你自己的答案。 – titaniumdecoy 2010-12-18 00:20:24

30

您發佈應該可以正常工作的代碼,我想。問題是,你把它放在哪裏?我會把它放到你推送的視圖控制器的-viewDidLoad方法中。如果你需要不同的按鈕,取決於你顯示的內容,那麼你可以在-viewWillAppear:方法中完成。

更新:其實,我覺得你需要改變

self.navigationController.navigationItem.rightBarButtonItem = doneButton; 

self.navigationItem.rightBarButtonItem = doneButton; 
+0

不,試過..但它是有幫助的。我把它移到viewDidAppear。似乎問題是每當一個新的視圖控制器被推到堆棧頂部時,右鍵獲取_HIDDEN_ – bobobobo 2009-11-24 04:28:45

+0

我已經更新了我的答案。我認爲你的代碼中存在一個錯誤。讓我知道這是否修復它。 – 2009-11-24 04:34:56

+0

不,我確實嘗試過,但是一旦新視圖控制器被推入,rightBarButtonItem仍然__disappears__。 – bobobobo 2009-11-24 04:38:13