以编程方式将 UIBarButtonItem 添加到 UINavigationBar

发布于 2024-11-25 04:49:02 字数 870 浏览 3 评论 0原文

我在 UIInterfaceBuilder 中放入了 UINavigationBar。我以模态方式呈现此视图,只想要一个 UIBackBarButton 返回到我的上一个视图。我声明了这个 UINavigationBar 的插座和属性。我认为在我的 viewDidLoad 方法中,我可以创建一个像这样的 UIBackButton :

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

但是我在 UINavigationBar 上看不到我的 UIBackBarButtonItem /代码>。我认为我在这里做错了什么,因为我认为我的 UINavigationBar 不知道我正在尝试以这种方式添加此 UIBackBarButtonItem 。我是否必须创建一个 NSArray,将按钮放入其中,然后为导航栏设置项目?

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

I dropped in a UINavigationBar in UIInterfaceBuilder. I present this view modally and just want a UIBackBarButton to return to my last view. I have an outlet and property to this UINavigationBar declared. I thought in my viewDidLoad method, I could create a UIBackButton like this:

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雨落□心尘 2024-12-02 04:49:02

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

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

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

self.navigationItem.backBarButtonItem = backButton;

self.navigationItem.leftBarButtonItem = backButton;

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 Documentation says:

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;
心不设防 2024-12-02 04:49:02

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

{
    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];    

}

会将 NextViewController 作为调用视图上的模态视图呈现,并且 NextViewController 将为其提供一个 navigationController。

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

使用后退按钮来关闭模态视图。希望有帮助。

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.

In The NextViewController implementation file all you need is this

- (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.

烟酉 2024-12-02 04:49:02

使用下面的代码片段:

//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
}

可用的不同样式类型有:

UIBarButtonItemStylePlain, UIBarButtonItemStyleBordered, UIBarButtonItemStyleDone

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
明明#如月 2024-12-02 04:49:02

您可以使用此设置器而无需创建新的 UIBarButtonItem:

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

You may use this setters without creation new UIBarButtonItem:

[self.navigationItem.leftBarButtonItem setAction:@selector(doBackButton:)];
[self.navigationItem.leftBarButtonItem setTarget:self];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文