如何在不使用标签栏的情况下在 UIViewController 之间切换?
我有一个 UINavigationController。在层次结构的第二层中,我想显示一个带有工具栏的视图控制器,在其中插入了分段控件。通过它,用户可以在同一页面的两个“视图”之间进行选择,我们可以将其称为 A 和 B(就像在日历应用程序中一样)。
当用户按下A段时,必须显示A视图。当用户按下B段时,必须显示B视图。
A 和 B 是复杂的视图,因此我更喜欢在两个单独的视图控制器(称为 AViewController 和 BViewController)中管理它们。
最初我想在 UITabBarViewController 中插入 AViewController 和 BViewController,但在 PushViewController:animated: 官方文档中我读到推送的视图控制器“不能是选项卡栏控制器的实例”。
你知道如何在不使用 UITabBarViewController 的情况下在 AViewController 和 BViewController 之间切换吗?
非常感谢!
I have a UINavigationController. In the 2nd level of my hierarchy, I want to show a view controller with a toolbar in which I inserted a segmented control. Through it the user can select between two "views" of the same page that we can call A and B (like in the Calendar application).
When the user press the A segment, the A view must be shown. When the user press the B segment, the B view must be shown.
A and B are complex views, so I prefer to manage them in two separate view controllers called AViewController and BViewController.
Initially I had thought to insert AViewController and BViewController in a UITabBarViewController, but in the pushViewController:animated: official documentation I read that the pushed view controller "cannot be an instance of tab bar controller."
Do you know how can I switch between AViewController and BViewController without using the UITabBarViewController?
Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会将两个视图添加为视图的子视图(称为 rootView),并使用 BringSubviewToFront 来显示它们:
I would add both of your views as subviews of a view (call it rootView) and use bringSubviewToFront to display them:
您可能想查看 UISegmentView,它将为您提供一些可用于更改视图内容的按钮。
另一种选择是使用“信息”按钮,在视图之间进行良好的翻转过渡。
第三个选项是让工具栏按钮将您的第二个视图作为模态视图,并在该屏幕上有一个关闭按钮,该按钮会自行关闭。
技术答案
创建一个容器视图控制器,其中包含 UISegment 视图和 2 个视图控制器实例变量
AViewController
和BViewController
。同样在主视图控制器中,有一个容器视图,为子视图设置适当的框架。在viewDidLoad
中实例化它们,但仅显示当前选定的一个...当
UISegmentView
更改时调用此方法。You probably want to looking at a UISegmentView which will give you a few buttons that you can use to alter view contents.
Another option would be using the Info button with a nice flip transition between the views.
A 3rd option would be to have a toolbar button bring your your second view as a modal view, and on that screen have a close button that dismisses itself.
A technical answer
Create a container view controller that holds the UISegment view and 2 view controller instance variables,
AViewController
andBViewController
. Also in your main view controller, have a container view that sets the appropriate frame for the child views. Instantiate both of them inviewDidLoad
but only show the one that is currently selected...Call this method when the
UISegmentView
changes.