将刷新 UIBarButtonItem 添加到从 NIB 加载的 UINavigationController

发布于 2024-09-28 08:26:15 字数 1198 浏览 11 评论 0原文

我在 UITabBarController 的第一个选项卡中加载了一个 UINavigationController。我想在 UINavigationItem 的右侧添加一个刷新按钮。我的模糊理解是,在将导航控制器添加到控件层次结构之前,必须在导航控制器上设置标题和按钮等属性。我向子导航控制器添加按钮没有问题,但我对如何更改根目录感到困惑,因为它是从笔尖加载的。

因此,在下面的 IB 屏幕截图中(哎呀,没有足够的声誉点,让我尝试用图表表示这一点......),我尝试向所选项目添加刷新按钮。

  • 文件所有者
  • 急救人员
  • 应用程序委托
  • 窗口
  • 标签栏控制器
    • 标签栏
    • 观察列表视图控制器
      • 导航栏
      • 视图控制器
        • 表格视图
        • 导航项
      • 标签栏项目
    • 另一个列表视图控制器
    • 等等
    • 等等

我尝试在 navigationController's viewDidLoad 中添加按钮,但没有任何帮助:

- (void)viewDidLoad {
[super viewDidLoad]; 

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
         initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
         target:self
             action:@selector(refresh:)];
}

我还尝试以编程方式将我的 ObservationListViewController 添加到 UITabBarController 中,并将按钮插入到该块中,但我远远超出了我的能力范围。

知道如何在从笔尖加载导航控制器的过程中注入添加按钮吗?

谢谢你!

I have a UINavigationController loading in the first tab of a UITabBarController. I'd like add a refresh button on the right side of the UINavigationItem. My dim understanding is that one has to set properties like titles and buttons on a nav controller before it's added to the control hierarchy. I have no problem adding buttons to the child navigation controller, but I'm stumped by how to change the root, since it's loading from the nib.

So in the IB screencap below (oops, not enough reputation points, let me try to diagram this....), I'm trying to add a refresh button to the selected item.

  • File's Owner
  • First Responder
  • App Delegate
  • Window
  • Tab Bar Controller
    • Tab Bar
    • Observation List View Controller
      • Navigation Bar
      • View Controller
        • Table View
        • Navigation Item
      • Tab Bar Item
    • Another List View Controller
    • etc.
    • etc.

I've tried to add the button in the navigationController's viewDidLoad, with no help:

- (void)viewDidLoad {
[super viewDidLoad]; 

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
         initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
         target:self
             action:@selector(refresh:)];
}

I've also tried to programmatically add my ObservationListViewController to the UITabBarController, and insert the button in that block, but I'm way out of my depth.

Any idea how I could inject adding a button into the process of loading a nav controller from the nib?

Thank you!

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

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

发布评论

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

评论(3

沫尐诺 2024-10-05 08:26:15

看起来您的 viewDidLoad 方法正在分配但没有释放栏按钮项目。你应该这样做:

UIBarButtonItem *button = [[UIBarButtonItem alloc]
     initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
     target:self
         action:@selector(refresh:)];
self.navigationItem.rightBarButtonItem = button;
[button release];

你不需要在导航控制器中执行此操作,而是在导航控制器中包含的根视图控制器中执行此操作。换句话说,将其放在“视图控制器”而不是“观察列表视图控制器”中。

您推送到导航控制器上的每个控制器都可以拥有自己的 self.navigationItem.whatever ,并且当推送新控制器时,导航控制器将显示该内容到它的堆栈上。

It looks like your viewDidLoad method is allocating but not releasing the bar button item. you should do this:

UIBarButtonItem *button = [[UIBarButtonItem alloc]
     initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
     target:self
         action:@selector(refresh:)];
self.navigationItem.rightBarButtonItem = button;
[button release];

You need to do this not in the nav controller, but in the root view controller that's contained within the navigation controller. In other words, put it in "View Controller" not "Observation List View Controller"

Each controller you push onto the nav controller can have it's own self.navigationItem.whatever and this will be displayed by the nav controller when the new controller is pushed onto its stack.

治碍 2024-10-05 08:26:15

致力于SWIFT 4.1

navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: 
UIBarButtonSystemItem.refresh, target: self, action: 
#selector(RootViewController.refresh))

Worked on SWIFT 4.1

navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: 
UIBarButtonSystemItem.refresh, target: self, action: 
#selector(RootViewController.refresh))
温馨耳语 2024-10-05 08:26:15

复制粘贴解决方案:

override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .refresh, target: self, action: #selector(refresh))
}

@objc func refresh() {
    print("Refreshing.")
}

Copy-paste solution:

override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .refresh, target: self, action: #selector(refresh))
}

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