从多个视图向 UINavigationBar 添加和删除 UIBarButtonItems

发布于 2024-11-08 14:34:38 字数 288 浏览 0 评论 0原文

我目前有一个主窗口设置为 UINavigationController (也是根视图控制器),并且我有两个视图。第一个视图是登录屏幕,第二个屏幕是表格视图屏幕。我希望发生的是根据显示的屏幕显示不同的 UIBarButtonItems。例如,当显示登录屏幕时,我希望在导航栏上显示一个左侧按钮(更具体地说,它是登录前的“设置”按钮)。用户登录后,我希望左侧按钮显示“注销”,右侧按钮显示“重新加载”按钮。我尝试以编程方式添加按钮,但它们不会显示。有什么建议吗?

另外,我已经在 IB 中完成了大部分工作,但我觉得以编程方式添加这些按钮可能会更容易。

I currently have a Main Window set up as a UINavigationController (also is the root view controller), and I have two views. The first view is the login screen, and the second screen is a table view screen. What I would like to have happen is to show different UIBarButtonItems based on what screen is showing up. For instance, when the logon screen is being displayed, I want a left button on the navigation bar to be displayed (more specifically, it'd be a Settings button before logging in). Once the user logs in, I want the left button to say "Logout" and the right to be a reload button. I've tried programmatically adding the buttons, but they won't show up. Any suggestions?

Also, I've gotten most of this done in IB, but I feel like it would probably be easier to add these buttons programmatically.

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

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

发布评论

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

评论(1

想挽留 2024-11-15 14:34:38

UIViewController 的所有子类都有一个名为 navigationItem。虽然它是只读的,但您可以更改其 属性。因此,在 LoginViewController 中,您将执行 self.navigationItem.leftBarButtonItem = /* Code to create the bar Button */ ,在 TableViewController 中也是如此。如果您需要更多详细信息,请告诉我。

All subclasses of UIViewController have a property called navigationItem. While it is readonly, you can alter its properties. So in LoginViewController, you would do self.navigationItem.leftBarButtonItem = /* Code to create the bar button */ and likewise within the TableViewController. Let me know if you need more detail.

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