简体   繁体   English

将按钮添加到导航工具栏

[英]Add a button to a navigation toolbar

How can a button be added to a navigation toolbar in an application which is navigation based? 如何在基于导航的应用程序中将按钮添加到导航工具栏?

The rootController is a tableView by default, and this is perfect. 默认情况下,rootController是一个tableView,这是完美的。 I would like to add a "share" button to the navigation controller so that I can attach it to my own method. 我想向导航控制器添加一个“共享”按钮,以便可以将其附加到我自己的方法上。

How is this done if the navigation bar is added in the code somewhere? 如果将导航栏添加到代码中的某处,该怎么做?

To do it in code, go to your views viewDidLoad method and create a UIBarButtonItem 为此,请转到视图的viewDidLoad方法并创建一个UIBarButtonItem

This code will create a button that says share like you wanted and put it on the right hand side of the navigation bar. 此代码将创建一个按钮,该按钮显示您想要的共享,并将其放在导航栏的右侧。

- (void)viewDidLoad {
     [super viewDidLoad];
     UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithTitle:@"Share"     style:UIBarButtonItemStyleBordered target:self action:@selector(share)];

    self.navigationItem.rightBarButtonItem = shareButton;
    [shareButton release];
}
UIBarButtonItem *shareButton = [[[UIBarButtonItem alloc] initWithTitle:@"Share" style:UIBarButtonItemStylePlain target:self action:@selector(yourShareAction)] autorelease];


self.navigationItem.rightBarButtonItem = shareButton;

This makes an add button, but you get the idea. 这样就创建了一个添加按钮,但是您明白了。

self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
  initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
  target: 
  self action:@selector(buttonPressed:)] 
  autorelease];

You can create an UIBarButtonItem and attach it to a root view controller's navigation item. 您可以创建一个UIBarButtonItem并将其附加到根视图控制器的导航项。 You add a target-action pair when you initialize the UIBarButtonItem object and that action message will be sent to the target on a user tapping on the button. 初始化UIBarButtonItem对象时,将添加一个目标操作对,并且该操作消息将在用户点击按钮时发送到目标。

So if you want the button when the table view is shown, set the UIBarButtonItem to your table view controller's navigationItem.rightBarButtomItem property. 因此,如果要在显示表格视图时使用按钮,请将UIBarButtonItem设置为表格视图控制器的navigationItem.rightBarButtomItem属性。

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

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