从多个视图向 UINavigationBar 添加和删除 UIBarButtonItems
我目前有一个主窗口设置为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
UIViewController
的所有子类都有一个名为navigationItem
。虽然它是只读的,但您可以更改其 属性。因此,在LoginViewController
中,您将执行self.navigationItem.leftBarButtonItem = /* Code to create the bar Button */
,在TableViewController
中也是如此。如果您需要更多详细信息,请告诉我。All subclasses of
UIViewController
have a property callednavigationItem
. While it is readonly, you can alter its properties. So inLoginViewController
, you would doself.navigationItem.leftBarButtonItem = /* Code to create the bar button */
and likewise within theTableViewController
. Let me know if you need more detail.