ios 如何向 UINavigationController 添加右键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1219779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:13:59  来源:igfitidea点击:

How to add a right button to a UINavigationController?

iphoneiosuinavigationcontrolleruinavigationbar

提问by Artilheiro

I am trying to add a refresh button to the top bar of a navigation controller with no success.

我正在尝试将刷新按钮添加到导航控制器的顶部栏,但没有成功。

Here is the header:

这是标题:

@interface PropertyViewController : UINavigationController {

}

Here is how I am trying to add it:

这是我尝试添加它的方式:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain
                                          target:self action:@selector(refreshPropertyList:)];      
        self.navigationItem.rightBarButtonItem = anotherButton;
    }
    return self;
}

回答by Louis Gerbarg

Try doing it in viewDidLoad. Generally you should defer anything you can until that point anyway, when a UIViewController is inited it still might be quite a while before it displays, no point in doing work early and tying up memory.

尝试在 viewDidLoad 中执行此操作。通常你应该推迟任何你能做到的事情,直到那个时候,当一个 UIViewController 被初始化时,它可能还需要很长时间才能显示出来,早点做工作并占用内存是没有意义的。

- (void)viewDidLoad {
  [super viewDidLoad];

  UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)];          
  self.navigationItem.rightBarButtonItem = anotherButton;
  // exclude the following in ARC projects...
  [anotherButton release];
}

As to why it isn't working currently, I can't say with 100% certainty without seeing more code, but a lot of stuff happens between init and the view loading, and you may be doing something that causes the navigationItem to reset in between.

至于为什么它目前不起作用,我不能在没有看到更多代码的情况下 100% 肯定地说,但是在 init 和视图加载之间发生了很多事情,并且您可能正在做一些导致 navigationItem 重置的事情之间。

回答by Adam

Try adding the button to the navigationItem of the view controller that is going to be pushed onto this PropertyViewControllerclass you have created.

尝试将按钮添加到将被推送到PropertyViewController您创建的此类的视图控制器的导航项中。

That is:

那是:

MainViewController *vc = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:@selector(showInfo) forControlEvents:UIControlEventTouchUpInside];
vc.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:infoButton] autorelease];

PropertyViewController *navController = [[PropertyViewController alloc] initWithRootViewController:vc];

Now, this infoButton that has been created programatically will show up in the navigation bar. The idea is that the navigation controller picks up its display information (title, buttons, etc) from the UIViewControllerthat it is about to display. You don't actually add buttons and such directly to the UINavigationController.

现在,以编程方式创建的这个 infoButton 将显示在导航栏中。这个想法是导航控制器从UIViewController它即将显示的信息中获取它的显示信息(标题、按钮等)。您实际上并未将按钮等直接添加到UINavigationController.

回答by Suragch

It seems that some people (like me) may come here looking for how to add a navigation bar button in the Interface Builder. The answer below shows how to do it.

似乎有些人(如我)可能会来这里寻找如何在 Interface Builder 中添加导航栏按钮。下面的答案显示了如何做到这一点。

Add a Navigation Controller to your Storyboard

将导航控制器添加到您的故事板

Select your View Controller and then in the Xcode menu choose Editor > Embed In > Navigation Controller.

选择您的视图控制器,然后在 Xcode 菜单中选择Editor > Embed In > Navigation Controller

enter image description here

在此处输入图片说明

Alternatively, you could add a UINavigationBarfrom the Object Library.

或者,您可以UINavigationBar从对象库中添加一个。

Add a Bar Button Item

添加条形按钮项

Drag a UIBarButtonItemfrom the Object Library to the top navigation bar.

UIBarButtonItem对象库中的a拖到顶部导航栏。

enter image description here

在此处输入图片说明

It should look like this:

它应该是这样的:

enter image description here

在此处输入图片说明

Set the Attributes

设置属性

You could double-click "Item" to change the text to something like "Refresh", but there is an actual icon for Refreshthat you can use. Just select the Attributes Inspector for the UIBarButtonItemand for System Itemchoose Refresh.

你可以双击“项目”来改变文字的东西,如“刷新”,但对于实际的图标刷新,您可以使用。只需选择 Attributes InspectorUIBarButtonItem并为System Item选择Refresh

enter image description here

在此处输入图片说明

That will give you the default Refresh icon.

这将为您提供默认的刷新图标。

enter image description here

在此处输入图片说明

Add an IB Action

添加 IB 操作

Control drag from the UIBarButtonItemto the View Controller to add an @IBAction.

控件从 拖到UIBarButtonItem视图控制器以添加@IBAction.

class ViewController: UIViewController {

    @IBAction func refreshBarButtonItemTap(sender: UIBarButtonItem) {

        print("How refreshing!")
    }

}

That's it.

就是这样。

回答by Manni

There is a default system button for "Refresh":

“刷新”有一个默认的系统按钮:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIBarButtonItem *refreshButton = [[[UIBarButtonItem alloc] 
                            initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
                            target:self action:@selector(refreshClicked:)] autorelease];
    self.navigationItem.rightBarButtonItem = refreshButton;

}

- (IBAction)refreshClicked:(id)sender {

}

回答by Vandit Mehta

You Can use this:

你可以使用这个:

Objective-C

目标-C

UIBarButtonItem *rightSideOptionButton = [[UIBarButtonItem alloc] initWithTitle:@"Right" style:UIBarButtonItemStylePlain target:self action:@selector(rightSideOptionButtonClicked:)];          
self.navigationItem.rightBarButtonItem = rightSideOptionButton;

Swift

迅速

let rightSideOptionButton = UIBarButtonItem()
rightSideOptionButton.title = "Right"
self.navigationItem.rightBarButtonItem = rightSideOptionButton

回答by fandro

For swift 2 :

对于快速 2 :

self.title = "Your Title"

var homeButton : UIBarButtonItem = UIBarButtonItem(title: "LeftButtonTitle", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("yourMethod"))

var logButton : UIBarButtonItem = UIBarButtonItem(title: "RigthButtonTitle", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("yourMethod"))

self.navigationItem.leftBarButtonItem = homeButton
self.navigationItem.rightBarButtonItem = logButton

回答by Gaurav Gilani

-(void) viewWillAppear:(BOOL)animated
{

    UIButton *btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnRight setFrame:CGRectMake(0, 0, 30, 44)];
    [btnRight setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
    [btnRight addTarget:self action:@selector(saveData) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barBtnRight = [[UIBarButtonItem alloc] initWithCustomView:btnRight];
    [barBtnRight setTintColor:[UIColor whiteColor]];
    [[[self tabBarController] navigationItem] setRightBarButtonItem:barBtnRight];

}

回答by vontell

Here is the solution in Swift (set options as needed):

这是 Swift 中的解决方案(根据需要设置选项):

var optionButton = UIBarButtonItem()
optionButton.title = "Settings"
//optionButton.action = something (put your action here)
self.navigationItem.rightBarButtonItem = optionButton

回答by user2999133

You can try

你可以试试

self.navigationBar.topItem.rightBarButtonItem = anotherButton;

回答by Jasarien

Why are you subclasses UINavigationController? There is no need to subclass it if all you need to do is add a button to it.

你为什么是子类UINavigationController?如果您只需要为其添加一个按钮,则无需对其进行子类化。

Set up a hierarchy with a UINavigationControllerat the top, and then in your root view controller's viewDidLoad:method: set up the button and attach it to the navigation item by calling

UINavigationController在顶部设置一个层次结构,然后在根视图控制器的viewDidLoad:方法中:设置按钮并通过调用将其附加到导航项

[[self navigationItem] setRightBarButtonItem:myBarButtonItem];