繁体   English   中英

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

[英]How do I add a button to my navigationController's right side after pushing another view controller in?

因此,在将视图控制器推送到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,并且所有内容,只是按钮永远不会出现在右侧..

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

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

self.navigationController.navigationItem.rightBarButtonItem = doneButton;

self.navigationItem.rightBarButtonItem = doneButton;

啊。 你必须做:

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

谁先打败了它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM