UINavigationItem BackButtonItem 不显示

发布于 2024-12-09 16:37:00 字数 880 浏览 1 评论 0原文

使用下面的代码我正在实现我自己的导航栏。由于某种原因,当我运行我的应用程序时,导航栏上的后退(左箭头)按钮没有显示任何内容。但是,如果我将代码更改为 leftBarButtonItem,该按钮就会出现。

// Draw Navigation Bar
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[navigationBar setDelegate:self];

UINavigationItem *navigationItem = [[UINavigationItem alloc] init];

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                style:UIBarButtonItemStylePlain
                                                               target:nil 
                                                               action:nil];
navigationItem.backBarButtonItem = backButton;
[navigationBar pushNavigationItem:navigationItem animated:NO];
[self.view addSubview:navigationBar];
[navigationBar release];
[backButton release];

Using the below code I am implementing my own Navigation Bar. For some reason when I run my app nothing is showing up for a back (left-arrow) button on the navigation bar. However if I change the code to leftBarButtonItem, the button does appear.

// Draw Navigation Bar
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[navigationBar setDelegate:self];

UINavigationItem *navigationItem = [[UINavigationItem alloc] init];

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                style:UIBarButtonItemStylePlain
                                                               target:nil 
                                                               action:nil];
navigationItem.backBarButtonItem = backButton;
[navigationBar pushNavigationItem:navigationItem animated:NO];
[self.view addSubview:navigationBar];
[navigationBar release];
[backButton release];

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

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

发布评论

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

评论(2

城歌 2024-12-16 16:37:01

backBarButtonItem 由父 ViewController 设置。
换句话说,它不是由您看到它的 ViewController 设置的,而是由它指向的 ViewController 设置的。因此,如果您的 ViewController 是第一个,它就不会有后退按钮。

另外,您正在自己创建一个 NavigtionBar。这通常不是正确的方法,UINavigationBar 不是像按钮或标签那样的 UI 元素。您应该使用 UINavigationController 来处理 ViewControllers 的所有推送和弹出操作。

The backBarButtonItem is set by the parent ViewController.
In other words, it's not set by the ViewController on which you see it but by the ViewController to which it points. So if your ViewController is the first in line, it just won't have a back button.

Also, you are creating a NavigtionBar by yourself. This is usually not the way to go, a UINavigationBar isn't a UI-Element like a Button or a Label. You should rather use a UINavigationController to handle all the pushing and popping of your ViewControllers.

[旋木] 2024-12-16 16:37:01

想通了:

// Draw Navigation Bar
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[navigationBar setDelegate:self];

UINavigationItem *navigationItem = [[UINavigationItem alloc] init];

UIButton *button = [UIButton buttonWithType:101];
// add selector
[button setTitle:@"Back" forState:UIControlStateNormal];

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:button];

navigationItem.leftBarButtonItem = backButton;
[navigationBar pushNavigationItem:navigationItem animated:NO];
[self.view addSubview:navigationBar];
[navigationBar release];
[backButton release];

Figured it out:

// Draw Navigation Bar
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[navigationBar setDelegate:self];

UINavigationItem *navigationItem = [[UINavigationItem alloc] init];

UIButton *button = [UIButton buttonWithType:101];
// add selector
[button setTitle:@"Back" forState:UIControlStateNormal];

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:button];

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