简体   繁体   English

使用UISegmentedControl在UIViewControllers之间切换

[英]Switching between UIViewControllers with UISegmentedControl

Right I have looked at a few SO questions on the subject and I am finding it difficult to come up with the correct solution here. 对,我已经看了几个关于这个问题的SO问题,我发现很难在这里提出正确的解决方案。

Requirements 要求

I have a UITabBar based application. 我有一个基于UITabBar的应用程序。 One of the tabs has a UINavigation controller with UISegmentedControl at the top allowing the user to switch between three different views. 其中一个选项卡有一个UINavigation控制器,顶部有UISegmentedControl ,允许用户在三个不同的视图之间切换。 Each view will have a UITableView which will allow the user to navigate to another view. 每个视图都有一个UITableView,允许用户导航到另一个视图。 These views should be pushed onto to the navigation controller. 应将这些视图推送到导航控制器。

Problem 问题

Now all the SO questions and Answers on the subject show how to switch between views. 现在关于该主题的所有SO问题和答案都显示了如何在视图之间切换。 However I need the view I switch to, to allow pushing of another view onto the navigation stack. 但是我需要切换到的视图,以允许将另一个视图推送到导航堆栈。 I am not sure this is even possible. 我不确定这是否可行。 I thought about UIViewController containment - however that would show a view being pushed onto the stack in a smaller window that the screen's bounds. 我想到了UIViewController包含 - 但是这会显示一个视图被推到堆栈中的一个较小的窗口,屏幕的界限。 Not what I am looking for. 不是我想要的。

Any ideas how I can solve this with storyboards and UIViewControllers? 有关故事板和UIViewControllers如何解决这个问题的任何想法?

UPDATE UPDATE

Here is what I am trying to do: In the screenshot the container area is where I need to load other view controllers into. 这是我想要做的:在屏幕截图中,容器区域是我需要加载其他视图控制器的地方。 The UISegment control cannot go into the navigation bar as that space is used for something else. UISegment控件无法进入导航栏,因为该空间用于其他内容。 So that's why I think UIViewController containment might be better here? 那么为什么我认为UIViewController遏制可能会更好?

在此输入图像描述

So even though this isn't using separate TableViewControllers , you can use different custom UIViews that are hidden by default and become visible when you select it's corresponding button. 因此,即使不使用单独的TableViewControllers ,您也可以使用默认隐藏的不同自定义UIViews ,并在选择相应按钮时变为可见。 This will unfortunately make it so you have all three view's logic in the same VC. 不幸的是,这将使你在同一个VC中拥有所有三个视图的逻辑。

To get around this, you can try setting up some delegates and mimicking the TableViewController logic separation by sending out the didSelectTableAtIndexPath , UIGesture touches, etc into classes outside the ViewController to help keep your code cleaner. 为了解决这个问题,您可以尝试设置一些委托并通过将didSelectTableAtIndexPathUIGesture touch等发送到ViewController外部的类来模仿TableViewController逻辑分离,以帮助保持代码更清晰。

User UITabBarController and hide the tab bar. 用户UITabBarController并隐藏标签栏。

- (void)viewDidLoad
{
     self.tabBar.hidden = YES;
}

Binding the segment control with method valueChanged 使用方法valueChanged绑定段控件

- (void)valueChanged:(UISegmentedControl *)seg
{
    if ([seg.selectedSegmentIndex == 0]) {
        self.selectedIndex = 0;
    } else if ([seg.selectedSegmentIndex == 1] {
        self.selectedIndex = 1;
    }
}

I achieve this by this way, I hope this will help. 我通过这种方式实现这一目标,我希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM