如何在不使用标签栏的情况下在 UIViewController 之间切换?

发布于 2024-08-24 18:08:48 字数 500 浏览 3 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(2

俯瞰星空 2024-08-31 18:08:48

我会将两个视图添加为视图的子视图(称为 rootView),并使用 BringSubviewToFront 来显示它们:

// display view A
[rootView bringSubviewToFront:AViewController.view];

// display view B
[rootView bringSubviewToFront:BViewController.view];

I would add both of your views as subviews of a view (call it rootView) and use bringSubviewToFront to display them:

// display view A
[rootView bringSubviewToFront:AViewController.view];

// display view B
[rootView bringSubviewToFront:BViewController.view];
迷爱 2024-08-31 18:08:48

您可能想查看 UISegmentView,它将为您提供一些可用于更改视图内容的按钮。

另一种选择是使用“信息”按钮,在视图之间进行良好的翻转过渡。

第三个选项是让工具栏按钮将您的第二个视图作为模态视图,并在该屏幕上有一个关闭按钮,该按钮会自行关闭。

技术答案

创建一个容器视图控制器,其中包含 UISegment 视图和 2 个视图控制器实例变量 AViewControllerBViewController。同样在主视图控制器中,有一个容器视图,为子视图设置适当的框架。在 viewDidLoad 中实例化它们,但仅显示当前选定的一个...

-(void)showAppropriateView {

     if([segment selectedIndex] == A_VIEW_SEGMENT) {
         [self.containerView addSubView:aViewController.view];
         [bViewController.view removeFromSuperView];
     } else {
         [self.containerView addSubView:bViewController.view];
         [aViewController.view removeFromSuperView];
     }
}

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 and BViewController. Also in your main view controller, have a container view that sets the appropriate frame for the child views. Instantiate both of them in viewDidLoad but only show the one that is currently selected...

-(void)showAppropriateView {

     if([segment selectedIndex] == A_VIEW_SEGMENT) {
         [self.containerView addSubView:aViewController.view];
         [bViewController.view removeFromSuperView];
     } else {
         [self.containerView addSubView:bViewController.view];
         [aViewController.view removeFromSuperView];
     }
}

Call this method when the UISegmentView changes.

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