在iOS中了解视图控制器嵌套 [英] Understanding View Controller Nesting in iOS

查看:200
本文介绍了在iOS中了解视图控制器嵌套的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去几天里,我一直在撕裂我的头发试图理解这个看似基本的iOS开发概念:

Ive been tearing my hair out over the last couple of days trying to understand this one seemingly basic concept of iOS development:


  • 如果我想在同一个screenful中显示和使用两个或多个视图控制器,则是:

  • If I want to have two or more View Controllers displayed and usable in the same "screenful", is this:


  1. 不建议

  2. 完全可以通过添加VC的代码

  3. 只是没有做到。


让我再说一下:

如果我想在iPad应用程序中有一个 UIView (A)占据屏幕左侧的大部分,第二个 UIView (B)占据屏幕左侧的大部分屏幕,我想添加一个按钮到 UIView B,当点击将使用模态转换向上滑动 UITableview 来替换UIView B,然后这个 UITableview 会像一个典型的 UITableviewController 表,典型的事件被发送到tableview控制器以推入一组新的项目,这是可能吗?

If I wanted to have, in an iPad app, a UIView (A) that takes up a large portion of the left side of the screen, and a second UIView (B) that takes up the rest of the right side of the screen, and I wanted to add a button to UIView B that when clicked would use the Modal transition to slide up a UITableview to replace UIView B, and this UITableview would then act like a typical UITableviewController whereby when the user picks an item from the table, the typical events are sent to the tableview controller to push in a new set of items, is this possible?

在我看来,如果Im已经能够很容易创建两个单独的UIViewControllers,并有一个按钮在一个VC模式提出第二个VC,为什么不能我结合这个功能,使一个VC有两个孩子VC,那些孩子VC处理自己的模态转换。

It just seems to me that if Im already able to easily create two separate UIViewControllers, and have a button in one VC modally bring up the second VC, why cant I combine this functionality so that one VC has two children VCs, and those children VCs handle their own modal transitions.

或者是这样的情况下最好的做法是简单地拥有一个处理所有事物的VC,然后在各种UI元素上进行各种点击后手动处理幻灯片进入/退出各种视图?

Or is the best practice in a case like this to simply have one VC that handles everything, and then manually handle animating the slides in / out of various views after various clicks on various UI elements?

正如你可以看到的,我认为Ive读了太多不同的,冲突的回答,类似于这个,我完全搞不清什么什么。

As you can tell, I think Ive read too many different, conflicting responses to questions similar to this that Ive gotten entirely confused about whats what anymore. If anyone out there understands what Im getting at and can lend a helping explanation or some pointers Id greatly appreciate it.

推荐答案

这个答案是什么意思?取决于你是否需要支持iOS 4。如果是,答案是:

The answer depends on whether you need to support iOS 4 or not. If yes, the answer is:

回答数字1 - 除了苹果自己的容器控制器,尤其是UITabBarController,UINavigationController,UISplitViewController,UIPageViewController和UIPopoverController ?)没有正确支持的方式让多个视图控制器在同一个屏幕上活动,管理视图的不同部分。视图控制器方法,如presentModalViewController等,都假设有一个前端视图控制器拥有整个屏幕,如果你尝试有多个视图控制器管理视图的不同部分,各种事情将

Answer number 1 - With the exception of Apple's own container controllers, notably UITabBarController, UINavigationController, UISplitViewController, UIPageViewController and UIPopoverController (did I miss any?) there is no properly supported way to have multiple view controllers active on the same screen, managing different portions of the view. View controller methods such as presentModalViewController, etc all work on the assumption that there is a single "frontmost" view controller that owns the whole screen, and if you try to have multiple view controllers manage different parts of the view, all kinds of things will break, such as forwarding of screen rotation events and resizing/positioning of views after a page transition.

但是如果你只需要支持iOS 5,答案是:

However if you only need to support iOS 5, the answer is:

回答号码2 - 没问题!只需确保所有子页面视图控制器正确地连接到管理整个页面的主视图控制器。这意味着除了控制器的视图是共同父视图的子视图之外,控制器本身应该是共同父控制器的子控制器。只要控制器的parentViewController属性都设置正确,你应该能够管理这种复合界面没有太多的麻烦。

Answer number 2 - No problem! Just make sure that all your sub-page-view controllers are correctly hooked up to a master view controller that manages the whole page. That means that in addition to the controllers' views being subviews of a common parent view, the controllers themselves should be child-controllers of a common parent controller. As long as the controller's parentViewController properties are all set correctly, you should be able to manage this kind of composite interface without too much hassle.

不幸的是,苹果只添加了公共设置对于iOS5中的childViewControllers和parentViewController。在iOS4和更早版本中,你仅限于苹果自己的容器控制器安排,并且不能创建自己的(至少,没有事件转发问题)。

Unfortunately, Apple only added public setters for childViewControllers and parentViewControllers in iOS5. In iOS4 and earlier you're limited to Apple's own container controller arrangements and cannot create your own (at least, not without event forwarding issues).

支持iOS4现在,回答数字3似乎是你最好的打赌:使用多个视图,但单个控制器构建您的界面。这不是太糟糕了。您可以创建管理继承自己的子视图的自定义视图子类(例如,没有规则说tableViewDataSource或委托必须是UIViewController子类,或者一个按钮IBAction必须是视图控制器上的方法,而不是另一个视图)。

So assuming you need to support iOS4 for now, answer number 3 seems like your best bet: Build your interface using multiple views but a single controller. This isn't too bad though. You can create custom view subclasses that manage heir own subviews (for example there's no rule that says a tableViewDataSource or delegate has to be a UIViewController subclass, or that a button IBAction has to be a method on a view controller instead of another view).

你会发现,通常构建到视图控制器中的大多数功能都可以内置到一个智能容器视图中,从而允许你拆分屏幕成为管理自己的内容的多个独立的控制器视图。

You'll actually find that most of the functionality you normally build into a view controller can be built into a "smart" container view instead, allowing you to split your screen into multiple standalone "controller-views" that manage their own contents.

唯一的一点是棘手的是做一些视图和下一个视图之间的转换。为此,你将无法使用标准presentModalViewController或pushViewController方法,你必须自己使用CATransitions或UIView动画。

The only bit that's tricky is doing stuff like transitions between one view and the next. For this you won't be able to use the standard presentModalViewController or pushViewController methods, you'll have to do the animations yourself using CATransitions or UIView animations.

这篇关于在iOS中了解视图控制器嵌套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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