gpt4 book ai didi

iphone - 向 UINavigationController 中的所有 View Controller 添加相同的按钮

转载 作者:IT王子 更新时间:2023-10-29 07:50:59 26 4
gpt4 key购买 nike

我有一个 UINavigationController(像向导页面一样使用),我以编程方式创建它,我需要显示一个“取消”按钮以取消任何 UIViewController 中的进程.

创建UINavigationController:

FirstVC *firstVC = [[[FirstVC alloc] initWithNibName:@"FirstPage" bundle:nil] autorelease];
firstVC.delegate = self;

navigationController = [[UINavigationController alloc] initWithRootViewController:firstVC];
[self.view addSubview:navigationController.view];

添加取消按钮:

UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelRequestNewLeave:)];
navigationController.topViewController.navigationItem.rightBarButtonItem = cancelButton;
[cancelButton release];

但是当我将第二个页面推送到 UINavigationController 时,UINavigationBar 上没有显示取消按钮。如果我回到第一页,取消按钮就在那里。因此,显然该按钮仅为第一个 View 添加。我相信这是因为我没有子类化 UINavigationController,因为我需要在 subview 中使用它。但我不知道如何在以编程方式创建的 UINavigationController 中设置 rightBarButtonItem

navigationController.topViewController.navigationItem.rightBarButtonItem = cancelButton;

有人可以阐明这一点吗?

提前致谢。

最佳答案

导航项是每个 View Controller 。导航栏从当前正在构建其 View 的 View Controller 的导航项中绘制其内容,该 View Controller 对应于导航 Controller 堆栈顶部的 View Controller 。

您基本上需要每个 View Controller 在其导航项中粘贴一个取消按钮。您可以执行以下任一操作:

  • 将代码复制粘贴到所有相关的 View Controller 中。
  • 将代码移动到实用函数或类中并调用它。
  • 为所有相关的 View Controller 创建一个公共(public)父类(super class),处理为其子类设置取消按钮。

关于iphone - 向 UINavigationController 中的所有 View Controller 添加相同的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6389094/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com