在推入另一个视图控制器后,如何向navigationController的右侧添加按钮?

bob*_*obo 11 iphone uinavigationcontroller

因此,在将视图控制器推送到tableView后立即执行,

// 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,并且所有内容,只是按钮永远不会出现在右侧..

Tho*_*ler 32

我认为你发布的代码应该可以正常工作.问题是,你把它放在哪里了?我会把它放到-viewDidLoad您正在推送的视图控制器的方法中.如果您需要不同的按钮,具体取决于您显示的内容,那么您可以在-viewWillAppear:方法中执行此操作.

更新:实际上,我认为你需要改变

self.navigationController.navigationItem.rightBarButtonItem = doneButton;
Run Code Online (Sandbox Code Playgroud)

self.navigationItem.rightBarButtonItem = doneButton;
Run Code Online (Sandbox Code Playgroud)


bob*_*obo 13

啊.你必须做:

- (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] ;
}

谁先打败了它.

  • 你应该接受托马斯的答案,而不是你自己的答案. (14认同)