ios 以编程方式将 UIBarButtonItem 添加到 UINavigationBar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6721169/
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 20:47:38  来源:igfitidea点击:

Adding a UIBarButtonItem programmatically to UINavigationBar

iosiphoneuinavigationbaruibarbuttonitem

提问by Crystal

I dropped in a UINavigationBarin UIInterfaceBuilder. I present this view modally and just want a UIBackBarButtonto return to my last view. I have an outlet and property to this UINavigationBardeclared. I thought in my viewDidLoadmethod, I could create a UIBackButtonlike this:

我投了UINavigationBar进去UIInterfaceBuilder。我以模态方式呈现此视图,只想UIBackBarButton返回到我的上一个视图。我有一个出口和财产UINavigationBar声明。我想在我的viewDidLoad方法中,我可以创建一个UIBackButton这样的:

UIBarButtonItem *backButton = 
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered 
                                              target:self 
                                              action:@selector(goBack)];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];

But I do not see my UIBackBarButtonItemon the UINavigationBar. I think I am doing something wrong here since I don't think my UINavigationBarknows I'm trying to add this UIBackBarButtonItemto it in this way. Would I have to do create an NSArray, put the button in it, and setItems for the NavigationBar instead?

但我没有看到我UIBackBarButtonItemUINavigationBar。我想我在这里做错了,因为我不认为我UINavigationBar知道我试图以UIBackBarButtonItem这种方式添加它。我是否必须创建一个NSArray,将按钮放入其中,然后为 NavigationBar 设置 setItems ?

I'm confused on how the navigationItem property works vs the setItems of the UINavigationBaras well. Any help would be appreciated. Thanks!

我对 navigationItem 属性与 setItems 的工作方式感到困惑UINavigationBar。任何帮助,将不胜感激。谢谢!

回答by Suhail Patel

You are trying to set the Back Button Item in a modal view which doesn't add a backBarButtonItem. This what causes the Button (or any sort of back button for that matter) not to show. The backBarButtonItem is mainly for use with Pushed View Controllers which have a Back Button added from the parent (next item below) when you push a new view controller (top item). The Apple UINavigationItem Documentationsays:

您正在尝试在不添加 backBarButtonItem 的模式视图中设置 Back Button Item。这导致按钮(或任何类型的后退按钮)不显示。backBarButtonItem 主要用于推送视图控制器,当您推送新的视图控制器(顶部项目)时,这些控制器具有从父级(下面的下一项)添加的后退按钮。在苹果UINavigationItem文档说:

When this item is the back item of the navigation bar—when it is the next item below the top item—it may be represented as a back button on the navigation bar. Use this property to specify the back button. The target and action of the back bar button item you set should be nil. The default value is a bar button item displaying the navigation item's title.

当该项目是导航栏的后退项目时——当它是顶部项目下方的下一个项目时——它可以表示为导航栏上的后退按钮。使用此属性指定后退按钮。您设置的后退栏按钮项的目标和操作应该为零​​。默认值为显示导航项标题的条形按钮项。

To get the Back Button on the left side like you wish, Try changing

要像您希望的那样在左侧获得后退按钮,请尝试更改

self.navigationItem.backBarButtonItem = backButton;

to

self.navigationItem.leftBarButtonItem = backButton;

回答by Rahul Sharma

making a call such as this from a view controller

从视图控制器进行这样的调用

{
    NextViewController* vcRootView = [[NextViewController alloc] initWithNibName:@"NextView" bundle:[NSBundle mainBundle]];
    UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:vcRootView];
    [vcRootView release];

    [self.navigationController presentModalViewController:navController animated:YES];
    [navController release];    

}

will present NextViewController as a Modal view on the calling view and NextViewController will have a navigationController for it.

将 NextViewController 作为调用视图上的 Modal 视图呈现,NextViewController 将为其提供一个 navigationController。

In The NextViewController implementation file all you need is this

在 NextViewController 实现文件中,您只需要这个

- (void)viewDidLoad {
    [super viewDidLoad];

    UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self 
                                                                  action:@selector(barButtonBackPressed:)];
    self.navigationItem.leftBarButtonItem = backButton;
    [backButton release];
}


-(void)barButtonBackPressed:(id)sender{
    [self dismissModalViewControllerAnimated:YES];
}

to have the back button to dismiss the modalview. Hope it helps.

让后退按钮关闭模态视图。希望能帮助到你。

回答by Jayprakash Dubey

Use below code snippet :

使用以下代码片段:

//Add button to NavigationController
UIBarButtonItem *backButton = 
 [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@“back”, @"")
                                  style:UIBarButtonItemStylePlain 
                                 target:self 
                                 action:@selector(goBack)];

self.navigationItem.leftBarButtonItem = backButton;

//Perform action on back Button
- (void) goBack {    // Go back task over-here
}

Different style types available are :

可用的不同样式类型有:

UIBarButtonItemStylePlain, UIBarButtonItemStyleBordered, UIBarButtonItemStyleDone

回答by Sergey Nikitin

You may use this setters without creation new UIBarButtonItem:

您可以在不创建新 UIBarButtonItem 的情况下使用此设置器:

[self.navigationItem.leftBarButtonItem setAction:@selector(doBackButton:)];
[self.navigationItem.leftBarButtonItem setTarget:self];