组合View Controller时遇到的一点问题

View Controller的组合应用其实很常见了,比如说Tab bar controller和Navigation view controller的组合使用,像这种一般都是Navigation view controller作为Tab bar controller的一个child view controller,对应了一个Tab bar item.

然后今天在Review 另外一组的一个产品的代码时,发现他们将Tab bar controller作为了一个普通view controller的child view controller在用,这个用法还是比较奇怪的说,通常基于Tab bar的应用程序都会选择将Tab bar controller作为一个根VC, 而这里面用了一个普通的View Controller作为根VC, 将Tab bar controller作为子VC, 类似下面这样:

[self addChildViewController:mTabBarController];
[self.view addSubview:mTabBarController.view];

据说是为了解决某个奇葩的问题才改成这样的。我只觉得这种方法有不妥之处,但具体哪里不对还真不知道,于是搜了一下官方的文档,看到下面这段:

When combining view controllers, however, the order of containment is important; only certain arrangements are valid. The order of containment, from child to parent, is as follows:

Content view controllers, and container view controllers that have flexible bounds (such as the page view controller)

Navigation view controller

Tab bar controller

Split view controller

Modal view controllers represent an interruption, they follow slightly different rules. You can present almost any view controller modally at any time. It is far less confusing to present a tab bar controller or navigation controller modally from a custom view controller.

也就是说,这种做法是不合理的,在组合的时候,parent-child之间是有约定的顺序的,Navigation view controller可以作为Tab bar controller的child view controller, 但反过来是不可以的。而Present就没有这个限制了。

当然,假如这么做了会带来什么后果,我暂时还没搞清楚。一般来说,多半是没有维护正确的层级,会导致一些特定的方法不能正确的传递到child view controller而引发一些问题。不过按照官方文档来,还是应该尽量避免这种用法的。

The End.

时间: 2024-08-09 23:51:48

组合View Controller时遇到的一点问题的相关文章

自定义View Controller转换动画

原文链接 : Introduction to Custom View Controller Transitions and Animations 原文作者 : joyce echessa 译文出自 : 开发技术前线 www.devtf.cn 译者 : kmyhy 观察 iOS 自带的 App,你会看到当你从一个视图导航到另一个视图时总是会显示各种各样的转换动画,以"主-从"视图为例(类似的程序有Messages App或者系统设置程序),一个轻扫动作能够让详情视图呈现在主视图之上,在呈

【IOS笔记】View Controller Basics

View Controller Basics   视图控制器基础 Apps running on iOS–based devices have a limited amount of screen space for displaying content and therefore must be creative in how they present information to the user. Apps that have lots of information to display

M2在奋斗之ios开发--View Controller pragramming guide for IOS中文版

About View Controllers 视图控制器是应用程序数据和其视觉外形之间的一个至关重要的链接.无论何时,应用程序显示一个用户界面,其显示的内容都是由一个或一组互相合作的视图控制器管理.因此,视图控制器给你建立的应用程序提供了骨架. iOS提供了很多内置的视图控制器类来支持标准用户界面块(piece),比如导航和标签栏.作为开发应用程序的一部分,你还可以实现一个或多个自定义控制器来显示应用程序的特定内容. 概述 在模型-视图-控制器(MVC)设计模式里,视图控制器是传统的控制器对象,

设置view controller到iPhone或者iPad模式

在写iOS程序时,view controller的显示大小以及控件大小的调节是在是一个费力的事,尤其是对于用mac本的童鞋,更难驾驭,这时我们可以根据需要设置专门针对iphone或者ipad的view controller的大小,可以修改设置项“Use Size Classes": 当选中时:view controller能同时适用iPhone和ipad的尺寸 未选中时:有提示框来指导是用iPhone的尺寸还是ipad得尺寸,运行时会根据选择的simulator变化

用Swift完成不同View Controller之间的切换

之前用objective-c开发时,页面之间的切换很容易.其实用swift没有很大的变化,如果你是用storyboard完成的界面,基本上是同样的方式,只不过在代码部分写成swift风格的就行了. 今天在实验开发一个简单的小程序时,却遇到了一些bug,后来还是求助stackoverflow上的大神解决了问题,在此做下记录. 我的程序结构是这样的,在一个页面A中有个按钮,然后点击按钮以后,切换到另一个页面B.A和B都在同一个storyboard中. 这里先说下通用的方法: 手动用代码建好的view

MVC模式(Model View Controller)下实现数据库的连接,对数据的删,查操作

MVC模式(Model View Controller): Model:DAO模型 View:JSP  在页面上填写java代码实现显示 Controller:Servlet 重定向和请求的转发: 若目标的相应页面不需要从request里面读取任何信息,则可以使用重定向,可以防止表单重复提交: ------------------------------------------------------------------------------------------------ Stude

Xcode7.2.1报错:Application windows are expected to have a root view controller at the end of application launch

原因:在较新的xcod上都会出现这种错误.在iOS5之前的版本,应用加载时,需要一个root view controller,在iOS5以下的版本会有MainWindow作为启动文件,iOS5以后的版本没有了.需要手动创建一个root view controller. 添加如下代码即可: self.window.rootViewController = [[UIViewController alloc]init];

Swift - iOS中各种视图控制器(View Controller)的介绍

在iOS中,不同的视图控制器负责不同的功能,采用不同的风格向用户呈现信息.下面对各个视图控制器做个总结: 1,标准视图控制器 - View Controller 这个控制器只是用来呈现内容.通常会用来作为子类,以向屏幕中添加逻辑. 2,导航控制器 - Navigation Controller 这个控制器呈现一个视图控制器的栈,应用程序可以在上面推入更多的视图控制器. 当新视图推入栈,或旧视图弹出栈时,导航控制器会以动画的形式(比如卷动)显示隐藏这些视图. 使用样例:系统“设置”应用程序 3,表

View Controller 生命周期的各个方法的用法

loadView; This is where subclasses should create their custom view hierarchy if they aren't using a nib. Should never be called directly.这是当他们没有正在使用nib视图页面,子类将会创建自己的自定义视图层.绝不能直接调用. - (void)awakeFromNib; 这个方法用的时候,outlet还没有连接起来,是view Controller刚从storyb