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

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

问题描述

我有一个标签栏 -> 导航控制器结构.在其中一个选项卡中,我想在两个 UIViewController 之间切换(准确地说是 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,它会在段值更改时弹出并推送适当的 ViewController.我认为这不是正确的做法,它还会破坏导航堆栈(当我点击栏项目时,导航控制器进入根控制器,这将不起作用).甚至还有另一个与 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: 等等.

It's your responsibility to send viewWillAppear: and so on.

您所说的偏移量可以使用以下方法进行调整:

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

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

编辑 2: 回应 tc. 的评论:

EDIT 2: In response to tc.'s comment:

来自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 在 UIViewController 之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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