使用UISegmentedControl在UIViewControllers之间切换 [英] Switch between UIViewControllers using UISegmentedControl

查看:543
本文介绍了使用UISegmentedControl在UIViewControllers之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个tabbar - > navigationcontroller结构。在其中一个选项卡中,我想在两个UIViewControllers之间切换( KalViewController 和UITableViewController准确无误),使用位于导航栏中的UISegmentedControl。

I have a tabbar -> navigationcontroller structure. In one of these tabs, I want to switch between two UIViewControllers (a KalViewController and a UITableViewController to be be exact), using a UISegmentedControl located in the Navigation Bar.

目前,我有第三个UIViewController,它会在段值更改时弹出并推送相应的ViewControllers。我不认为这是正确的方法,它也会破坏导航堆栈(当我点击条形项目时,导航控制器进入根控制器,这将无法工作)。而且还有另一个与Kal组件相关的错误。

Currently, I have a third UIViewController, that pops and pushes the appropriate ViewControllers on segment value change. I don't think thats the right way to do it and it also destroys the navigation stack (when I tap on the bar item, the navigation controller goes the root controller, which won't work). And there's even another bug, related to the Kal Component.

那么,正确的方式是什么?

推荐答案

正确的方法是让控制器处理 UISegmentedControl 将控制器的视图添加为子视图。

The right way to do it is to have the controller handling the UISegmentedControl add the views of the controllers as subviews.

[self.view addSubview:controller.view];

您有责任发送 viewWillAppear:和等等。

编辑:您所谈论的偏移量可以使用以下方式进行调整:

The offset you're talking about can be adjusted using:

controller.view.frame = CGRectMake(x, y, width, height);

编辑2:响应tc。的评论:

来自 UISplitViewController的文档


拆分视图控制器插入应用程序窗口及其子视图控制器之间。因此,到可见视图控制器的所有消息都必须流经拆分视图控制器。这通常可以像您期望的那样工作,并且消息流应该相对直观。例如,仅当相应的子视图控制器实际出现在屏幕上时,才会发送视图外观和消失消息。因此,当首先以纵向方向显示拆分视图控制器时,它将调用viewWillAppear:和viewDidAppear:仅初始显示的视图控制器的方法。使用弹出窗口显示的视图控制器在显示弹出窗口或分割视图控制器旋转到横向方向之前不会接收这些消息。

A split view controller interposes itself between the application’s window and its child view controllers. As a result, all messages to the visible view controllers must flow through the split view controller. This works generally as you might expect and the flow of messages should be relatively intuitive. For example, view appearance and disappearance messages are sent only when the corresponding child view controller actually appears on screen. Thus, when a split view controller is first displayed in a portrait orientation, it calls the viewWillAppear: and viewDidAppear: methods of only the view controller that is shown initially. The view controller that is presented using a popover does not receive those messages until the popover is shown or until the split view controller rotates to a landscape orientation.

这不是神奇的,也没有理由不能自己编写类似的控制器。事实上我已经完成了它并且工作正常。

This is not magical and there is no reason why you wouldn't be able to write a similar controller yourself. In fact I've done it and it worked just fine.

这篇关于使用UISegmentedControl在UIViewControllers之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆