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-10-13 18:23:31