“self.navigationItem.rightBarButtonItem”不工作

发布于 2024-12-06 05:48:09 字数 562 浏览 0 评论 0原文

我有以下代码直接取自 Apple 的 NavBar 示例代码。我将其放入我的应用程序中以模态方式呈现的视图的 viewDidLoad 方法中,但它不起作用。

UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"AddTitle", @"")
                                                               style:UIBarButtonItemStyleBordered
                                                              target:self
                                                              action:@selector(addAction:)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;

有什么建议吗?

I have the following code taken straight from the NavBar sample code from Apple. I put this in the viewDidLoad method for a view in my app that is being presented modally, and it wont work.

UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"AddTitle", @"")
                                                               style:UIBarButtonItemStyleBordered
                                                              target:self
                                                              action:@selector(addAction:)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;

Any Suggestions?

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

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

发布评论

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

评论(4

柠檬色的秋千 2024-12-13 05:48:10

好的解释解决方案:

presentModalViewController:animated:以模态方式呈现一个viewController,它没有UINavigationBar,因此您可以做一些事情:

  1. 在viewController的笔尖中添加一个UINavigationBar并添加那里的“添加”按钮以及您需要设置的一切。
  2. 您可以使用 pushViewController:animated: 以模态方式显示将位于导航堆栈上的 viewController,并使用 UINavigationBar 供您添加按钮(
  3. 如果您的第一个 viewController 不是UINavigationController,使用 pushViewController:animated: 无法解决该问题,因此您可以以 viewController 作为模态呈现 UINavigationController rootViewController:
 YourViewController *viewController =[[[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil] autorelease];
    UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:viewController] autorelease];
    [self.navigationController presentModalViewController:navController animated:YES];

希望这些有帮助

Okay explained solution:

presentModalViewController:animated: presents a viewController modally, which does not have a UINavigationBar, so you can do some things:

  1. Add a UINavigationBar in your viewController's nib and add the "Add" button there and everything you need to setup.
  2. You can use pushViewController:animated: to show the viewController modally which will be on the navigation stack and have the UINavigationBar for you to add your button
  3. If your first viewController is not a UINavigationController, using pushViewController:animated: won't solve it, so you can present a UINavigationController modally with your viewController as the rootViewController:
 YourViewController *viewController =[[[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil] autorelease];
    UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:viewController] autorelease];
    [self.navigationController presentModalViewController:navController animated:YES];

Hope any of this helps

枉心 2024-12-13 05:48:10

您需要在呈现其他视图的页面上使用这些代码行。

sceondController *obj=[[[sceondController alloc] initWithNibName:@"sceondController" bundle:nil] autorelease];
        UINavigationController *navController=[[[UINavigationController alloc] initWithRootViewController:obj] autorelease];

        [self.navigationController presentModalViewController:navController animated:NO];

并在第二个视图中使用与制作导航按钮相同的代码。

也许它可以解决您的问题。

You need to use these lines of code on the page where you present the other view.

sceondController *obj=[[[sceondController alloc] initWithNibName:@"sceondController" bundle:nil] autorelease];
        UINavigationController *navController=[[[UINavigationController alloc] initWithRootViewController:obj] autorelease];

        [self.navigationController presentModalViewController:navController animated:NO];

and in second view use same code which you are using for making navigation button.

May be it resolves your problem.

云巢 2024-12-13 05:48:10

我假设你的视图控制器实际上是一个 UINavigationController 并且其他一切都已就位。在这种情况下,我会改变两件事。

  1. 我不会自动释放 UIBarButtonItem。对于视图控制器来说,这往往不可靠,因此将按钮添加到要在清理时释放的事物列表中

  2. 我将使用 setter 函数来设置按钮。这是我在导航控制器中运行的代码

    clearAllButton = [[UIBarButtonItem alloc] initWithTitle:@"全部清除" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonPressed:)];

    [[self navigationItem] setRightBarButtonItem:clearAllButton];

I assume your view controller is actually a UINavigationController and everything else is in place. In which case I would change two things.

  1. I wouldn't autorelease the UIBarButtonItem. That tends to be unreliable with view controllers so add the button to your list of things to dealloc at cleanup

  2. I would use the setter function to set the button. Here is my code that works in my navigation controller

    clearAllButton = [[UIBarButtonItem alloc] initWithTitle:@"Clear All" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonPressed:)];

    [[self navigationItem] setRightBarButtonItem:clearAllButton];

定格我的天空 2024-12-13 05:48:10

在真实设备中运行您的应用程序。在 iOS6 中,它无法在模拟器上运行。

Run your app in real device. In iOS6 it is not working on simulator.

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