【问题标题】:UISegmentedControl in a PopoverController with multiple view controllers具有多个视图控制器的 PopoverController 中的 UISegmentedControl
【发布时间】:2011-10-25 04:55:36
【问题描述】:

我想在 PopoverController 中嵌入一个 UISegmentedControl,类似于这个 SO 问题中描述的内容:UISegmentedControl embedded in a UINavigationBar/Item

不同之处在于,对于要在弹出窗口中显示的每个视图,我都有不同的视图控制器,具体取决于分段控件上的选定索引。我不确定我会怎么做。每当我尝试在根视图控制器上推送新视图时, UISegmentedControl 就会消失。我只想在两个视图控制器之间切换,同时保持 UISegmentedControl 可见。这甚至可能吗?

提前致谢!

【问题讨论】:

    标签: iphone ios uinavigationcontroller uipopovercontroller uisegmentedcontrol


    【解决方案1】:

    如果它对 segmentBar 上的每个段使用不同的 viewController,则必须使用容器 viewController 将每个 viewController 的视图添加为自身的子视图,或者将其视图设置为 viewController 的视图看法。例如:

    UIViewController* containerController = [[[UIViewController alloc] init] autorelease];
    
    
    //Inside the viewDidLoad of the the ContainerController class, do the following:
    
    //Initialize all three viewControllers
    UIViewController* test1 = [[[UIViewController alloc] init] autorelease];
    UIViewController* test1 = [[[UIViewController alloc] init] autorelease];
    UIViewController* test1 = [[[UIViewController alloc] init] autorelease];
    
    //set up the segment and add it to the container's navBar's title view.
    [segmentedControl addTarget:self action:@selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged];
    
    
    - (void)segmentValueChanged:(id)sender 
    {
    
        //if first tab selected
        [self.view removeAllSubviews];
        [self.view addSubview:test1.view];
    
        //if second tab selected
        [self.view removeAllSubviews];
        [self.view addSubview:test2.view];
    
        //if third tab selected
        [self.view removeAllSubviews];
        [self.view addSubview:test3.view];
    
    }
    

    您可以只设置self.view = test1.view,而不是将其添加为子视图。显然,您将使用容器视图来初始化 navController 并将该 navController 放在弹出框内。希望这可以帮助!

    【讨论】:

    • 感谢@Bittu 的回答,这实际上是我最终做的。只是想看看是否有更优雅的方式来做到这一点。可能不会。再次感谢!
    【解决方案2】:

    如果您使用presentModalViewController 方法在屏幕上显示您的新视图控制器,它将始终覆盖整个屏幕及其下方的任何内容。这就是它的工作原理。

    根据文档:

    在 iPhone 和 iPod touch 设备上,modalViewController 的视图是 始终呈现全屏。在 iPad 上,演示文稿取决于 modalPresentationStyle 属性中的值。

    实现它并且仍然能够控制视图控制器如何定位的方法是创建自己的呈现方法。

    【讨论】:

    • 首先,感谢您的输入。但恐怕我不明白你的意思。我正在展示一个 UIPopoverController,所以我使用了函数 presentPopoverFromRect: inView: allowedArrowDirections: animated: 我将 UINavigationController(包含我的自定义视图控制器)作为弹出框的内容 VC 传递。如果我错了请纠正我,但我不认为 presentModalViewController 可以在这里应用
    猜你喜欢
    • 1970-01-01
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    相关资源
    最近更新 更多